Cody

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

Solution 1979847

Submitted on 17 Oct 2019
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)

y = 0 1 0 0 0 1 1 0 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)

y = Columns 1 through 30 0 0 0 1 0 0 1 1 1 0 0 0 1 1 1 0 0 0 0 0 1 1 1 0 0 0 1 0 0 1 Columns 31 through 60 0 1 0 1 1 1 1 1 0 1 0 1 1 0 1 1 0 0 0 1 1 0 1 0 0 1 1 0 1 0 Columns 61 through 90 1 1 1 0 1 1 1 0 1 1 0 0 1 0 0 1 1 0 0 1 0 0 1 1 0 0 0 1 0 1 Columns 91 through 120 0 1 1 1 0 0 0 1 0 0 0 1 1 1 0 1 0 1 0 0 1 1 1 1 0 0 1 1 1 1 Columns 121 through 150 0 0 1 1 0 1 1 0 1 1 1 1 1 1 0 0 1 1 1 0 1 0 0 1 0 1 1 0 1 0 Columns 151 through 180 1 1 1 1 1 1 1 1 0 0 1 0 1 1 0 0 0 1 1 0 0 0 0 1 0 1 0 1 0 0 Columns 181 through 210 0 0 1 0 1 1 0 1 0 1 0 1 0 0 1 1 1 0 1 0 1 0 0 1 1 0 1 1 1 0 Columns 211 through 240 0 1 0 0 1 0 0 1 1 0 0 0 1 1 0 1 1 0 1 0 1 0 0 1 1 1 0 0 0 0 Columns 241 through 270 0 0 0 0 1 1 1 0 0 0 0 0 1 0 1 0 1 1 0 0 0 1 1 1 0 1 0 0 0 1 Columns 271 through 300 0 1 1 0 1 1 0 0 1 1 0 1 0 0 1 0 0 1 0 0 0 1 1 1 0 1 1 0 0 1 Columns 301 through 330 0 1 0 0 0 0 0 1 1 1 1 0 1 1 1 0 1 0 1 0 0 0 0 1 1 0 1 0 0 0 Columns 331 through 360 0 0 0 1 1 1 0 0 1 1 0 1 0 1 1 0 1 0 1 0 0 1 1 1 1 1 0 1 0 1 Columns 361 through 390 0 1 1 1 0 0 1 0 1 1 0 0 0 0 0 1 0 1 0 0 1 0 1 1 0 1 0 0 0 0 Columns 391 through 420 0 1 1 1 1 0 1 0 0 0 1 0 0 0 0 1 0 0 0 1 1 0 0 0 0 1 0 1 0 0 Columns 421 through 450 0 1 0 1 0 1 0 1 1 1 0 1 0 0 0 0 0 1 1 0 1 1 1 0 0 1 0 1 0 0 Columns 451 through 480 1 1 1 1 1 0 0 1 1 1 1 1 0 1 1 0 0 1 1 1 1 1 0 0 1 1 1 0 1 1 Columns 481 through 510 0 1 1 1 0 0 1 0 0 0 1 1 1 1 0 0 0 0 1 0 1 0 1 1 0 0 1 0 0 0 Columns 511 through 540 0 0 1 1 0 0 1 1 1 0 0 1 0 0 1 0 0 0 0 0 1 0 0 1 1 1 0 1 0 1 Columns 541 through 570 0 1 1 0 0 1 1 0 1 0 1 0 1 1 0 0 0 0 1 1 1 0 0 1 1 0 0 1 0 1 Columns 571 through 600 1 0 0 0 1 0 1 0 1 0 1 0 0 1 1 0 0 0 0 1 0 0 0 1 1 1 1 0 0 0 Columns 601 through 630 1 1 0 1 0 1 0 0 1 1 0 1 0 0 0 1 1 0 0 1 0 1 0 0 1 0 0 1 0 1 Columns 631 through 660 1 0 1 1 0 1 1 1 1 0 0 0 1 1 0 1 1 1 0 1 1 0 0 1 0 1 1 1 1 0 Columns 661 through 690 0 1 0 1 1 0 1 1 1 0 0 0 1 1 0 0 1 0 1 0 1 0 1 1 1 1 0 0 1 1 Columns 691 through 720 0 0 1 0 0 0 1 0 1 0 0 1 1 0 1 1 0 1 0 1 0 1 1 0 0 0 0 1 1 1 Columns 721 through 750 1 0 1 1 0 1 1 1 0 0 0 1 1 1 1 1 0 1 0 0 1 1 0 0 1 0 0 1 0 1 Columns 751 through 780 1 0 1 1 1 0 0 1 1 1 0 1 0 0 1 0 0 0 1 1 1 0 0 1 1 0 0 0 1 1 Columns 781 through 810 0 0 0 0 1 1 1 0 0 1 1 0 0 1 1 0 1 0 0 0 1 0 0 1 0 1 1 0 0 0 Columns 811 through 840 0 0 1 1 1 0 1 1 1 0 0 0 1 1 0 1 1 1 1 0 1 0 1 0 1 0 0 1 1 0 Columns 841 through 870 1 1 1 1 0 0 1 0 1 1 1 0 0 1 0 1 1 0 1 0 1 1 1 0 0 0 1 0 0 0 Columns 871 through 900 1 0 1 0 0 1 1 1 0 1 1 1 1 1 0 1 1 0 0 1 1 0 1 0 0 1 0 1 0 1 Columns 901 through 930 1 1 0 0 1 0 0 0 0 0 1 0 0 1 1 0 0 0 1 0 0 1 0 1 1 1 1 0 0 0 Columns 931 through 960 1 1 0 0 0 0 1 1 0 0 1 0 1 0 0 1 1 0 1 1 1 0 1 1 0 0 1 0 1 1 Columns 961 through 990 1 1 1 1 0 0 0 0 0 1 0 1 1 1 0 0 0 1 1 0 0 1 0 0 1 1 1 0 1 1 Columns 991 through 1000 0 0 0 1 0 0 0 1 0 0

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)

y = Columns 1 through 30 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 Columns 31 through 60 0 0 0 0 0 0 1 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 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 0 0 0 0 Columns 91 through 120 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 0 0 Columns 121 through 150 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 Columns 151 through 180 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 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 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 0 0 0 Columns 241 through 270 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 Columns 271 through 300 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 301 through 330 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 0 0 0 0 0 Columns 331 through 360 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 0 0 Columns 361 through 390 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 0 0 0 0 Columns 391 through 420 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 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 1 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 0 0 0 0 0 0 0 0

Assertion failed.