Cody

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

Solution 2472461

Submitted on 4 Jun 2020
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   Fail
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)

a = 8 3 10 1 y = 1 0 1 0 0 0 0 1 0 1

Assertion failed.

2   Fail
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)

a = Columns 1 through 15 114 522 769 494 177 538 509 759 719 354 707 584 135 601 266 Columns 16 through 30 587 804 673 124 985 802 743 295 743 86 555 520 345 112 872 Columns 31 through 45 873 537 219 619 128 703 323 975 437 49 1000 485 394 223 992 Columns 46 through 60 437 778 102 626 325 671 317 666 846 438 626 407 908 194 882 Columns 61 through 75 622 171 377 1000 273 936 103 329 228 448 645 255 735 847 563 Columns 76 through 90 703 17 717 617 412 893 327 989 325 944 186 693 164 786 102 Columns 91 through 105 824 363 734 40 510 149 983 886 919 528 376 768 929 224 167 Columns 106 through 120 262 285 439 880 250 598 918 670 686 71 779 734 710 126 140 Columns 121 through 135 329 476 384 624 714 242 325 704 135 847 237 32 322 243 381 Columns 136 through 150 851 696 994 989 893 857 810 644 578 18 792 59 489 88 265 Columns 151 through 165 968 825 250 561 327 152 559 512 876 473 298 290 595 647 905 Columns 166 through 180 261 28 956 19 56 90 134 727 927 849 890 551 768 587 512 Columns 181 through 195 416 375 86 932 359 122 428 638 989 156 702 266 521 174 777 Columns 196 through 210 705 24 139 665 366 111 684 879 399 261 93 838 982 611 432 Columns 211 through 225 36 687 487 178 229 164 322 638 729 997 605 643 517 139 139 Columns 226 through 240 570 182 824 410 541 874 307 668 666 179 580 16 663 535 636 Columns 241 through 255 850 706 193 508 437 599 448 94 989 737 744 386 80 661 600 Columns 256 through 270 576 655 187 622 918 331 627 661 317 168 365 680 89 1000 569 Columns 271 through 285 705 771 974 67 119 780 328 22 485 504 260 3 164 925 186 Columns 286 through 300 953 419 475 670 535 196 78 842 207 12 989 679 886 563 394 Columns 301 through 315 729 888 231 5 111 175 679 898 871 165 400 839 24 227 403 Columns 316 through 330 299 511 970 605 414 730 905 244 795 283 519 215 410 579 510 Columns 331 through 345 910 962 191 567 95 405 352 659 577 359 673 635 211 651 786 Columns 346 through 360 542 568 815 410 758 626 378 77 991 675 749 515 72 454 519 Columns 361 through 375 762 236 396 868 226 729 13 449 624 889 394 955 856 393 752 Columns 376 through 390 693 367 219 514 799 963 35 310 941 445 18 471 490 135 447 Columns 391 through 405 603 380 881 820 622 865 912 179 362 744 381 23 265 178 788 Columns 406 through 420 943 558 347 64 346 294 907 335 835 392 217 706 116 626 990 Columns 421 through 435 758 19 532 700 97 933 57 453 260 130 8 245 315 228 691 Columns 436 through 450 579 325 594 848 834 35 552 443 421 648 588 231 662 1000 53 Columns 451 through 465 187 384 163 948 408 897 925 945 874 273 881 765 490 312 556 Columns 466 through 480 959 603 884 569 796 747 154 250 975 84 881 328 752 559 673 Columns 481 through 495 99 697 668 446 484 87 599 113 144 453 984 208 279 384 70 Columns 496 through 500 761 783 223 809 911 y = Columns 1 through 30 0 0 1 0 1 0 0 1 0 0 0 1 1 0 0 1 1 1 1 0 0 1 1 1 0 0 0 1 0 0 Columns 31 through 60 0 1 0 0 1 1 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 1 0 0 1 1 0 1 0 Columns 61 through 90 0 0 0 1 0 0 1 0 0 1 1 1 0 0 0 0 1 1 0 1 0 0 0 1 0 1 1 1 1 1 Columns 91 through 120 0 0 1 1 1 0 1 0 1 0 0 1 1 0 0 0 0 0 0 0 1 1 1 1 0 1 0 0 1 0 Columns 121 through 150 0 1 0 1 0 1 0 1 0 1 0 0 0 1 1 0 0 0 1 1 0 0 0 1 0 0 0 0 1 0 Columns 151 through 180 0 1 0 1 0 1 0 0 0 0 0 0 1 1 1 0 1 1 0 0 1 0 0 1 1 0 1 1 1 0 Columns 181 through 210 0 1 0 0 0 1 1 0 0 0 1 0 1 1 0 1 0 0 0 0 0 0 0 0 0 0 1 1 0 0 Columns 211 through 240 1 0 0 0 1 0 1 0 1 0 0 0 1 1 0 1 1 1 1 0 1 0 0 0 0 1 1 0 0 0 Columns 241 through 270 0 1 1 1 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 1 1 0 0 1 1 0 0 0 0 Columns 271 through 300 0 0 1 0 0 0 0 0 1 0 0 0 1 0 1 0 0 0 0 1 0 0 0 1 1 0 0 1 1 0 Columns 301 through 330 0 0 0 0 0 0 1 0 0 1 0 1 0 0 1 0 1 0 0 0 0 1 1 0 1 0 1 1 1 0 Columns 331 through 360 1 0 0 0 1 0 0 0 0 0 0 0 0 0 1 1 1 0 0 0 0 1 0 1 0 0 0 0 1 0 Columns 361 through 390 0 1 1 0 1 1 1 0 0 0 0 0 0 0 1 1 1 1 0 1 1 0 0 1 0 1 0 0 0 0 Columns 391 through 420 0 1 1 1 0 1 0 0 1 1 0 0 1 0 1 0 1 1 0 1 0 1 0 1 0 1 0 0 1 0 Columns 421 through 450 1 0 0 0 0 0 0 1 0 0 0 1...

Assertion failed.

3   Fail
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)

a = 61 346 125 493 147 158 355 68 152 74 1 156 127 283 297 418 27 70 296 79 y = Columns 1 through 30 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 Columns 31 through 60 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 61 through 90 1 0 0 0 0 0 0 1 0 1 0 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 Columns 91 through 120 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 121 through 150 0 0 0 0 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 Columns 151 through 180 0 1 0 0 0 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 181 through 210 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 211 through 240 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 241 through 270 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 271 through 300 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 Columns 301 through 330 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 331 through 360 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 Columns 361 through 390 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 391 through 420 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 Columns 421 through 450 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 451 through 480 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 481 through 500 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0

Assertion failed.