Cody

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

Solution 192381

Submitted on 17 Jan 2013 by Aurelien Queffurust
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)

r = 9 r = 4 r = 3 r = 5

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)

r = 125 r = 652 r = 11 r = 555 r = 882 r = 808 r = 65 r = 281 r = 843 r = 357 r = 19 r = 540 r = 291 r = 944 r = 349 r = 888 r = 238 r = 943 r = 591 r = 291 r = 599 r = 675 r = 820 r = 27 r = 85 r = 687 r = 505 r = 978 r = 972 r = 704 r = 96 r = 8 r = 176 r = 589 r = 308 r = 65 r = 915 r = 24 r = 806 r = 820 r = 621 r = 886 r = 589 r = 13 r = 746 r = 798 r = 205 r = 558 r = 849 r = 779 r = 37 r = 111 r = 656 r = 392 r = 502 r = 535 r = 600 r = 61 r = 298 r = 379 r = 228 r = 47 r = 63 r = 877 r = 737 r = 540 r = 363 r = 619 r = 312 r = 74 r = 634 r = 873 r = 390 r = 167 r = 948 r = 484 r = 283 r = 833 r = 407 r = 577 r = 610 r = 916 r = 648 r = 710 r = 247 r = 676 r = 565 r = 683 r = 446 r = 674 r = 441 r = 305 r = 721 r = 359 r = 816 r = 892 r = 740 r = 26 r = 427 r = 360 r = 869 r = 542 r = 688 r = 443 r = 264 r = 733 r = 203 r = 238 r = 317 r = 94 r = 654 r = 555 r = 680 r = 239 r = 937 r = 701 r = 422 r = 9 r = 804 r = 341 r = 144 r = 619 r = 217 r = 440 r = 951 r = 578 r = 854 r = 512 r = 443 r = 169 r = 603 r = 76 r = 731 r = 112 r = 487 r = 390 r = 819 r = 562 r = 205 r = 405 r = 64 r = 783 r = 312 r = 703 r = 916 r = 704 r = 625 r = 326 r = 935 r = 200 r = 246 r = 349 r = 512 r = 884 r = 98 r = 996 r = 936 r = 279 r = 803 r = 759 r = 322 r = 386 r = 283 r = 266 r = 754 r = 435 r = 520 r = 285 r = 394 r = 125 r = 116 r = 679 r = 321 r = 462 r = 460 r = 683 r = 799 r = 224 r = 240 r = 31 r = 473 r = 305 r = 942 r = 803 r = 368 r = 118 r = 245 r = 768 r = 745 r = 518 r = 425 r = 723 r = 217 r = 209 r = 686 r = 974 r = 441 r = 821 r = 526 r = 644 r = 564 r = 468 r = 195 r = 776 r = 642 r = 810 r = 363 r = 308 r = 692 r = 492 r = 731 r = 144 r = 284 r = 63 r = 613 r = 212 r = 141 r = 226 r = 193 r = 761 r = 964 r = 200 r = 530 r = 20 r = 435 r = 611 r = 217 r = 872 r = 887 r = 417 r = 799 r = 286 r = 949 r = 139 r = 327 r = 270 r = 623 r = 782 r = 132 r = 986 r = 625 r = 584 r = 100 r = 266 r = 497 r = 508 r = 390 r = 844 r = 365 r = 510 r = 679 r = 304 r = 868 r = 559 r = 769 r = 627 r = 626 r = 7 r = 13 r = 613 r = 59 r = 242 r = 112 r = 28 r = 221 r = 329 r = 324 r = 733 r = 297 r = 994 r = 387 r = 762 r = 863 r = 591 r = 302 r = 364 r = 345 r = 31 r = 807 r = 242 r = 349 r = 22 r = 541 r = 84 r = 956 r = 595 r = 539 r = 373 r = 479 r = 696 r = 143 r = 585 r = 955 r = 847 r = 931 r = 337 r = 487 r = 144 r = 211 r = 495 r = 804 r = 477 r = 387 r = 285 r = 965 r = 60 r = 675 r = 991 r = 171 r = 998 r = 359 r = 526 r = 97 r = 727 r = 721 r = 609 r = 394 r = 158 r = 507 r = 342 r = 908 r = 519 r = 829 r = 198 r = 973 r = 651 r = 683 r = 881 r = 159 r = 502 r = 858 r = 153 r = 195 r = 243 r = 480 r = 849 r = 841 r = 392 r = 442 r = 172 r = 408 r = 627 r = 969 r = 638 r = 869 r = 136 r = 642 r = 217 r = 590 r = 668 r = 895 r = 13 r = 411 r = 159 r = 939 r = 845 r = 453 r = 815 r = 364 r = 502 r = 513 r = 881 r = 468 r = 706 r = 745 r = 72 r = 187 r = 726 r = 846 r = 541 r = 58 r = 696 r = 414 r = 117 r = 996 r = 427 r = 736 r = 707 r = 213 r = 899 r = 451 r = 818 r = 215 r = 361 r = 98 r = 650 r = 833 r = 741 r = 156 r = 493 r = 859 r = 265 r = 924 r = 74 r = 872 r = 309 r = 612 r = 603 r = 622 r = 899 r = 907 r = 516 r = 517 r = 670 r = 774 r = 30 r = 750 r = 902 r = 890 r = 374 r = 893 r = 845 r = 618 r = 832 r = 854 r = 164 r = 147 r = 338 r = 367 r = 817 r = 520 r = 402 r = 28 r = 483 r = 402 r = 515 r = 450 r = 54 r = 423 r = 137 r = 61 r = 387 r = 61 r = 432 r = 649 r = 362 r = 602 r = 97 r = 245 r = 407 r = 264 r = 126 r = 112 r = 770 r = 387 r = 787 r = 162 r = 890 r = 780 r = 37 r = 592 r = 74 r = 511 r = 642 r = 543 r = 381 r = 135 r = 427 r = 34 r = 675 r = 920 r = 389 r = 529 r = 441 r = 257 r = 3 r = 830 r = 439 r = 335 r = 190 r = 329 r = 219 r = 263 r = 926 r = 852 r = 703 r = 794 r = 258 r = 850 r = 553 r = 497 r = 558 r = 986 r = 14 r = 867 r = 660 r = 816 r = 770 r = 528 r = 388 r = 765 r = 184 r = 751 r = 195 r = 662 r = 975 r = 422 r = 949 r = 478 r = 160 r = 931 r = 441 r = 308 r = 742 r = 712 r = 458 r = 418 r = 22 r = 802 r = 627 r = 472 r = 515 r = 654 r = 878 r = 851 r = 210 r = 729 r = 927 r = 568 r = 753 r = 126 r = 949 r = 372 r = 584 r = 843 r = 11 r = 905 r = 899 r = 678 r = 932 r = 160 r = 114 r = 726 r = 335 r = 119 r = 529 r = 184 r = 646 r = 431 r = 181 r = 398 r = 665 r = 729 r = 923 r = 417 r = 80 r = 911 r = 916 r = 45 r = 237 r = 654 r = 680 r = 14 r = 577 r = 512 r = 466 r = 685 r = 633 r = 764 r = 560 r = 783 r = 262 r = 158 r = 926 r = 810 r = 447 r = 479 r = 56 r = 399 r = 986 r = 291 r = 418 r = 949 r = 662 r = 802 r = 564 r = 650 r = 819 r = 641 r = 883 r = 246 r = 403 r = 981 r = 42 r = 885 r = 491 r = 56 r = 233 r = 135 r = 294 r = 438 r = 771 r = 945 r = 415 r = 117 r = 648 r = 829 r = 747 r = 770 r = 908 r = 82 r = 447 r = 547 r = 746 r = 388 r = 44 r = 47 r = 663 r = 924 r = 308 r = 890 r = 992 r = 151 r = 557 r = 849 r = 982 r = 115 r = 170 r = 280 r = 395 r = 64 r = 954 r = 144 r = 907 r = 280 r = 531 r = 780 r = 58 r = 829 r = 515 r = 349 r = 944 r = 491 r = 228 r = 255 r = 937 r = 754 r = 695 r = 815 r = 694 r = 688 r = 551 r = 862 r = 334 r = 943 r = 547 r = 959 r = 148 r = 781 r = 46 r = 296 r = 895 r = 37 r = 948 r = 169 r = 499 r = 289 r = 899 r = 24 r = 330 r = 463 r = 760 r = 183 r = 854 r = 913 r = 128 r = 477 r = 203 r = 499 r = 973 r = 125 r = 500 r = 292 r = 639 r = 851 r = 358 r = 359 r = 590 r = 219 r = 722 r = 360 r = 390 r = 594 r = 236 r = 318 r = 420 r = 994 r = 186 r = 108 r = 127 r = 900 r = 883 r = 764 r = 433 r = 526 r = 778 r = 399 r = 443 r = 960 r = 286 r = 759 r = 909 r = 292 r = 630 r = 66 r = 31 r = 748 r = 554 r = 747 r = 269 r = 284 r = 403 r = 498 r = 41 r = 631 r = 674 r = 707 r = 200 r = 486

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)

r = 301 r = 360 r = 144 r = 455 r = 440 r = 322 r = 157 r = 235 r = 300 r = 323 r = 446 r = 387 r = 498 r = 75 r = 154 r = 28 r = 353 r = 39 r = 492 r = 411