Cody

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

Solution 2579112

Submitted on 18 Jun 2020 by Nikolaos Nikolaou
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)

y = 1×10 logical array 0 0 1 0 1 1 0 1 0 0

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)

y = 1×1000 logical array Columns 1 through 45 1 0 0 1 0 0 0 0 0 0 1 0 1 0 1 0 0 1 0 0 0 0 0 1 0 1 0 1 1 1 1 0 1 0 1 1 1 1 1 0 1 1 0 0 1 Columns 46 through 90 0 1 1 0 0 1 1 0 0 0 0 1 0 0 0 1 1 1 1 0 1 0 1 0 1 1 1 1 1 1 1 0 1 1 0 0 1 1 1 0 0 1 0 0 0 Columns 91 through 135 0 0 1 0 0 0 0 0 1 0 0 1 1 0 1 1 0 1 1 1 0 1 0 1 0 0 0 0 1 1 1 1 1 0 1 0 1 1 1 0 0 1 1 0 0 Columns 136 through 180 0 1 0 0 1 0 1 1 0 0 1 1 0 0 0 1 0 0 1 1 1 0 0 1 0 1 1 1 0 1 1 1 1 0 0 0 0 1 0 0 0 0 0 0 1 Columns 181 through 225 0 0 0 0 1 0 1 0 0 1 1 1 0 1 0 0 1 1 0 1 0 1 1 0 1 0 0 0 1 0 1 0 0 1 0 1 0 1 1 1 1 0 0 0 0 Columns 226 through 270 0 1 0 0 0 0 0 0 0 0 0 1 1 1 0 1 1 0 1 0 1 1 1 0 1 0 1 1 1 1 1 0 0 0 1 1 1 0 0 1 1 1 0 0 0 Columns 271 through 315 0 0 0 0 1 0 0 1 1 1 1 0 0 0 1 0 1 0 1 1 0 0 1 0 1 1 0 0 0 1 1 0 1 0 0 1 1 0 1 0 1 1 0 0 0 Columns 316 through 360 0 1 0 0 1 0 0 1 1 0 0 0 0 0 0 0 1 0 1 0 0 0 1 1 1 1 0 1 0 1 1 1 1 1 0 0 1 0 1 1 0 1 0 0 1 Columns 361 through 405 0 1 1 0 1 0 0 0 1 1 0 0 1 0 1 0 0 0 0 0 0 1 0 1 1 1 1 0 1 1 0 0 1 1 0 0 1 1 1 1 0 0 1 1 1 Columns 406 through 450 0 1 0 1 0 0 1 1 1 0 0 1 0 1 1 1 1 1 1 0 1 1 0 1 1 1 1 1 0 1 0 0 0 0 0 0 1 1 1 0 0 1 0 1 1 Columns 451 through 495 1 0 0 1 1 1 1 1 0 1 0 0 0 1 0 1 1 1 1 0 0 0 1 0 1 1 1 1 1 0 0 1 1 1 0 0 0 1 1 0 0 1 1 1 0 Columns 496 through 540 0 0 1 1 0 0 1 0 0 1 0 1 1 0 1 0 0 0 0 1 1 1 1 1 0 1 0 1 1 1 0 1 1 1 0 1 1 0 1 1 1 1 0 1 1 Columns 541 through 585 1 0 0 1 0 0 0 0 0 0 1 0 1 0 0 1 1 1 0 0 1 1 0 0 1 1 1 0 0 0 0 0 1 0 1 0 1 0 1 0 0 1 1 0 0 Columns 586 through 630 1 1 1 1 1 1 1 0 0 1 1 1 1 0 0 1 0 1 0 0 0 0 1 0 1 1 0 0 0 1 0 1 1 1 1 1 1 0 1 0 1 1 1 0 1 Columns 631 through 675 0 0 1 0 0 0 1 0 0 0 0 1 0 0 1 1 1 1 1 1 0 0 0 0 0 0 0 1 1 0 1 0 1 1 1 1 0 0 0 1 1 0 1 1 1 Columns 676 through 720 1 1 0 0 1 1 0 1 1 0 1 0 1 1 0 1 0 1 0 0 0 1 0 0 0 0 1 0 0 1 0 1 1 1 1 1 0 0 1 0 1 1 1 0 1 Columns 721 through 765 1 0 1 0 1 0 0 0 1 0 0 0 0 1 0 1 0 0 0 0 0 1 0 0 0 1 0 1 1 0 0 0 0 0 0 0 1 1 1 0 1 0 0 1 1 Columns 766 through 810 1 1 0 0 0 1 1 1 0 1 0 0 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 0 0 1 1 1 0 1 0 0 1 1 0 1 0 0 Columns 811 through 855 0 1 0 1 0 0 1 1 0 1 0 0 0 1 1 1 0 0 0 0 0 1 0 1 0 1 1 1 0 1 0 1 1 1 1 0 1 0 1 1 0 1 1 0 1 Columns 856 through 900 1 1 1 1 1 0 0 0 0 1 0 0 0 0 1 1 0 0 1 0 0 1 1 1 0 0 1 1 0 1 1 0 0 1 0 0 1 0 1 1 0 0 1 1 0 Columns 901 through 945 1 0 1 0 1 1 0 1 0 1 1 0 0 0 1 1 1 1 1 0 0 0 1 0 1 0 1 1 0 1 1 0 0 1 0 1 0 1 0 0 0 1 1 1 0 Columns 946 through 990 1 1 0 1 0 1 1 0 0 0 1 1 1 1 1 1 0 1 1 0 0 1 1 1 0 1 0 1 0 0 1 1 1 1 1 1 0 1 1 1 0 1 1 1 1 Columns 991 through 1,000 0 0 1 0 0 0 0 0 1 0

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)

y = 1×500 logical array Columns 1 through 45 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 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 46 through 90 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 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 Columns 91 through 135 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 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 136 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 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 Columns 181 through 225 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 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 Columns 226 through 270 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 1 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 Columns 271 through 315 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 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 316 through 360 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 Columns 361 through 405 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 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 406 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 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 451 through 495 0 0 0 0 0 0 0 0 1 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 0 0 0 0 0 0 0 0 0 0 Columns 496 through 500 0 0 0 0 0