Cody

# Problem 1090. Create a random logical vector of N elements of which M are true.

Solution 173977

Submitted on 6 Dec 2012 by Evan
This solution is locked. To view this solution, you need to provide a solution of the same size or smaller.

### Test Suite

Test Status Code Input and Output
1   Pass
%% n = 10; m = 4; y = random_binary(n,m); assert(islogical(y) && isequal(sum(y),m) && abs(std(diff(y)>0)-0.45)<0.2)

u = 1 8 9 2 6

2   Pass
%% n = 1000; m = 500; y = random_binary(n,m); assert(islogical(y) && isequal(sum(y),m) && abs(std(diff(y)>0)-0.45)<0.05)

u = Columns 1 through 8 223 553 381 178 176 63 503 748 Columns 9 through 16 114 866 318 965 990 940 892 267 Columns 17 through 24 258 347 918 190 941 434 753 761 Columns 25 through 32 771 164 390 435 28 135 513 472 Columns 33 through 40 576 837 47 861 543 579 891 897 Columns 41 through 48 924 566 648 270 590 328 747 213 Columns 49 through 56 375 623 30 525 311 424 610 295 Columns 57 through 64 296 405 969 599 437 317 801 769 Columns 65 through 72 605 20 613 560 6 255 974 168 Columns 73 through 80 289 218 153 401 518 805 834 574 Columns 81 through 88 45 141 238 1000 406 150 997 564 Columns 89 through 96 745 620 500 719 717 393 89 488 Columns 97 through 104 954 629 630 885 111 373 826 351 Columns 105 through 112 764 458 100 611 916 501 749 197 Columns 113 through 120 225 535 330 346 363 53 332 948 Columns 121 through 128 554 79 467 366 528 914 95 714 Columns 129 through 136 686 545 211 819 200 638 770 828 Columns 137 through 144 664 949 658 507 809 17 853 561 Columns 145 through 152 917 995 313 46 552 70 978 120 Columns 153 through 160 787 694 27 44 91 752 75 387 Columns 161 through 168 660 159 900 188 266 976 194 571 Columns 169 through 176 598 132 107 793 534 814 219 558 Columns 177 through 184 867 162 345 372 734 62 839 272 Columns 185 through 192 287 353 246 751 109 614 612 294 Columns 193 through 200 985 241 208 750 699 301 203 818 Columns 201 through 208 34 192 69 182 446 821 716 386 Columns 209 through 216 226 172 249 742 687 907 441 663 Columns 217 through 224 51 684 214 222 129 308 606 792 Columns 225 through 232 54 709 151 703 278 297 844 735 Columns 233 through 240 879 202 419 68 134 856 563 529 Columns 241 through 248 530 48 408 889 555 459 334 838 Columns 249 through 256 35 438 193 460 103 101 763 462 Columns 257 through 264 300 160 113 973 608 411 556 155 Columns 265 through 272 261 362 78 568 326 131 195 240 Columns 273 through 280 569 550 947 259 426 112 344 952 Columns 281 through 288 204 339 736 161 359 953 628 700 Columns 289 through 296 644 273 50 43 310 516 788 741 Columns 297 through 304 702 83 582 909 505 185 216 309 Columns 305 through 312 682 726 175 539 231 400 136 679 Columns 313 through 320 355 951 758 201 854 945 377 509 Columns 321 through 328 615 852 994 690 379 464 9 718 Columns 329 through 336 693 177 510 398 263 327 896 77 Columns 337 through 344 321 581 265 778 810 260 737 722 Columns 345 through 352 41 869 807 504 895 477 833 23 Columns 353 through 360 591 447 806 67 786 471 531 392 Columns 361 through 368 777 457 303 667 31 905 698 417 Columns 369 through 376 989 432 979 85 631 140 369 474 Columns 377 through 384 847 603 640 723 580 913 975 798 Columns 385 through 392 123 835 385 511 399 514 232 549 Columns 393 through 400 180 14 291 497 245 254 791 680 Columns 401 through 408 934 319 227 312 198 575 672 958 Columns 409 through 416 848 118 72 169 927 356 143 58 Columns 417 through 424 454 886 110 533 421 64 55 607 Columns 425 through 432 86 880 782 996 795 316 955 727 Columns 433 through 440 370 304 887 491 893 536 744 899 Columns 441 through 448 939 654 825 235 689 167 849 626 Columns 449 through 456 383 217 653 521 56 139 104 97 Columns 457 through 464 860 156 850 635 730 931 766 371 Columns 465 through 472 799 21 567 523 596 977 71 815 Columns 473 through 480 325 546 875 739 397 15 285 796 Columns 481 through 488 268 870 418 922 633 632 251 673 Columns 489 through 496 817 256 498 920 433 409 541 410 Columns 497 through 504 755 425 987 461 943 367 619 90 Columns 505 through 512 33 873 102 87 26 548 933 637 Columns 513 through 520 165 843 910 520 962 24 378 108 Columns 521 through 528 636 12 475 499 713 210 94 808 Columns 529 through 536 444 492 19 413 937 708 315 335 Columns 537 through 544 678 935 789 926 73 643 277 388 Columns 545 through 552 415 448 257 283 642 912 921 279 Columns 553 through 560 3 652 305 851 772 725 490 42 Columns 561 through 568 865 61 93 942 422 988 656 96 Columns 569 through 576 487 276 783 584 762 671 537 342 Columns 577 through 584 864 126 157 618 11 99 174 395 Columns 585 through 592 179 299 715 333 517 790 80 117 Columns 593 through 600 728 932 39 649 427 888 993 812 Columns 601 through 608 105 696 964 502 233 803 348 81 Columns 609 through 616 133 768 183 621 115 286 82 665 Columns 617 through 624 609 547 149 779 352 677 616 376 Columns 625 through 632 456 191 384 66 374 746 121 49 Columns 633 through 640 820 431 389 485 911 404 589 380 Columns 641 through 642 884 557

3   Pass
%% n = 500; m = 20; y = random_binary(n,m); assert(islogical(y) && isequal(sum(y),m) && abs(std(diff(y)>0)-0.18)<0.05)

u = Columns 1 through 16 223 469 175 444 66 116 34 491 117 74 210 126 24 5 121 435 Columns 17 through 32 209 288 294 343 351 441 350 482 214 31 349 118 388 379 48 389 Columns 33 through 48 277 399 414 162 373 500 57 378 380 88 494 445 432 279 347 159 Columns 49 through 64 272 425 495 138 303 53 101 278 62 141 160 496 52 292 174 493 Columns 65 through 80 360 221 107 36 336 83 230 100 443 412 60 89 418 87 474 219 Columns 81 through 96 21 6 77 188 255 361 405 113 397 353 337 310 227 384 318 438 Columns 97 through 112 462 90 165 18 177 43 329 114 147 205 15 424 271 293 243 437 Columns 113 through 128 142 297 19 481 37 115 359 51 423 148 229 307 298 184 473 143 Columns 129 through 144 3 371 315 468 13 421 29 131 457 486 27 381 202 430 295 327 Columns 145 through 160 169 478 401 155 398 362 193 463 97 433 393 78 483 452 283 427 Columns 161 through 176 429 275 20 236 304 465 308 459 69 263 369 30 305 377 395 137 Columns 177 through 192 475 73 391 460 419 309 181 436 179 291 82 387 269 392 370 233 Columns 193 through 208 72 286 167 420 59 472 235 124 237 428 104 346 55 250 45 375 Columns 209 through 224 348 416 480 68 228 258 477 246 203 33 311 170 61 368 183 499 Columns 225 through 240 79 207 479 266 344 54 390 133 129 490 86 58 464 8 200 461 Columns 241 through 256 302 282 355 94 376 422 485 91 196 497 208 190 249 185 434 50 Columns 257 through 272 341 407 256 44 345 64 32 187 158 446 455 139 163 316 161 28 Columns 273 through 288 105 4 127 290 356 99 276 408 145 70 476 335 372 364 489 180 Columns 289 through 304 14 458 46 112 409 321 109 411 400 296 198 450 102 440 71 306 Columns 305 through 319 264 42 339 403 261 456 270 285 394 154 12 120 25 67 251