Cody

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

Solution 274108

Submitted on 5 Jul 2013 by andrea84
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)

``` A = 1 1 1 1 0 0 0 0 0 0 index = 7 9 10 2 5 6 3 4 8 1 A = 0 0 0 1 0 0 1 1 0 1 ```

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)

``` A = Columns 1 through 16 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 Columns 17 through 32 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 Columns 33 through 48 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 Columns 49 through 64 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 Columns 65 through 80 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 Columns 81 through 96 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 Columns 97 through 112 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 Columns 113 through 128 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 Columns 129 through 144 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 Columns 145 through 160 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 Columns 161 through 176 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 Columns 177 through 192 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 Columns 193 through 208 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 Columns 209 through 224 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 Columns 225 through 240 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 Columns 241 through 256 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 Columns 257 through 272 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 Columns 273 through 288 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 Columns 289 through 304 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 Columns 305 through 320 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 Columns 321 through 336 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 Columns 337 through 352 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 Columns 353 through 368 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 Columns 369 through 384 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 Columns 385 through 400 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 Columns 401 through 416 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 Columns 417 through 432 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 Columns 433 through 448 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 Columns 449 through 464 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 Columns 465 through 480 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 Columns 481 through 496 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 Columns 497 through 512 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 Columns 513 through 528 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 529 through 544 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 545 through 560 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 561 through 576 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 577 through 592 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 593 through 608 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 609 through 624 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 625 through 640 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 641 through 656 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 657 through 672 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 673 through 688 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 689 through 704 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 705 through 720 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 721 through 736 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 737 through 752 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 753 through 768 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 769 through 784 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 785 through 800 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 801 through 816 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 817 through 832 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 833 through 848 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 849 through 864 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 865 through 880 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 881 through 896 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 897 through 912 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 913 through 928 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 929 through 944 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 945 through 960 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 961 through 976 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 977 through 992 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 993 through 1000 0 0 0 0 0 0 0 0 index = Columns 1 through 8 728 663 982 661 609 685 18 987 Columns 9 through 16 979 751 249 789 519 584 938 200 Columns 17 through 24 493 362 597 117 417 178 78 757 Columns 25 through 32 155 431 680 193 983 324 869 706 Columns 33 through 40 106 6 905 210 353 253 274 172 Columns 41 through 48 388 379 333 715 993 994 581 929 Columns 49 through 56 870 286 386 594 595 761 771 11 Columns 57 through 64 145 514 824 850 795 129 535 198 Columns 65 through 72 677 394 533 975 446 899 51 659 Columns 73 through 80 882 40 586 234 620 363 515 649 Columns 81 through 88 195 169 846 821 810 995 787 744 Columns 89 through 96 17 977 376 521 603 94 27 273 Columns 97 through 104 269 616 284 90 211 267 605 23 Columns 105 through 112 577 835 834 639 928 770 497 654 Columns 113 through 120 109 374 368 591 475 741 301 77 Columns 121 through 128 15 244 25 986 16 785 416 861 Columns 129 through 136 26 513 293 341 207 343 668 410 Columns 137 through 144 647 28 154 665 940 607 522 865 Columns 145 through 152 745 465 782 372 596 102 720 ...```

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)

``` A = Columns 1 through 16 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 Columns 17 through 32 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 Columns 33 through 48 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 49 through 64 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 65 through 80 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 81 through 96 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 97 through 112 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 113 through 128 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 129 through 144 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 145 through 160 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 161 through 176 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 177 through 192 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 193 through 208 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 209 through 224 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 225 through 240 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 241 through 256 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 257 through 272 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 273 through 288 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 289 through 304 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 305 through 320 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 321 through 336 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 337 through 352 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 353 through 368 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 369 through 384 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 385 through 400 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 401 through 416 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 417 through 432 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 433 through 448 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 449 through 464 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 465 through 480 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 481 through 496 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 497 through 500 0 0 0 0 index = Columns 1 through 16 120 173 338 330 457 434 25 142 423 273 425 71 304 263 74 409 Columns 17 through 32 38 163 432 295 155 113 314 433 347 357 276 321 284 56 342 479 Columns 33 through 48 441 440 399 365 356 218 221 444 336 87 125 447 200 307 262 27 Columns 49 through 64 490 292 280 437 455 414 395 460 35 140 23 150 394 42 416 442 Columns 65 through 80 77 397 456 226 371 76 51 62 237 10 157 85 159 326 16 340 Columns 81 through 96 86 396 47 231 500 254 324 482 192 366 122 32 351 164 123 315 Columns 97 through 112 2 183 151 3 375 282 167 279 299 24 176 488 104 90 111 238 Columns 113 through 128 445 149 446 429 195 477 79 473 270 392 228 102 467 476 374 285 Columns 129 through 144 214 495 387 286 407 41 381 153 463 187 95 339 370 480 232 413 Columns 145 through 160 96 281 184 376 88 80 98 297 124 410 453 19 182 278 417 320 Columns 161 through 176 166 234 129 427 361 49 136 6 152 203 162 134 412 146 328 31 Columns 177 through 192 240 106 83 209 15 255 165 197 81 245 257 188 82 471 28 306 Columns 193 through 208 73 119 143 50 325 466 210 204 13 171 224 33 384 288 355 229 Columns 209 through 224 385 303 309 319 105 63 316 308 7 386 296 461 36 196 233 251 Columns 225 through 240 1 403 58 494 419 219 485 160 323 498 305 491 452 275 363 220 Columns 241 through 256 97 269 424 112 349 474 4 406 107 333 272 121 352 128 135 30 Columns 257 through 272 310 131 101 465 248 265 379 380 205 212 180 12 300 343 430 68 Columns 273 through 288 69 421 185 398 216 481 156 436 198 302 489 115 301 290 78 26 Columns 289 through 304 84 174 45 190 420 439 264 64 227 215 137 18 53 332 358 298 Columns 305 through 320 246 17 483 344 252 8 243 334 451 486 345 283 496 261 404 462 Columns 321 through 336 258 454 268 388 194 103 435 169 43 393 360 39 92 350 274 449 Columns 337 through 352 422 382 206 312 239 353 11 250 401 335 470 144 450 54 91 322 Columns 353 through 368 60 207 147 139 267 75 391 126 127 327 468 48 464 59 109 289 Columns 369 through 384 114 208 5 178 89 367 145 225 369 291 373 443 318 181 487 359 Columns 385 through 400 438 383 138 484 235 199 175 20 55 311 100 348 52 201 378 400 Columns 401 through 416 141 93 179 426 193 94 331 377 186 61 99 130 66 492 158 202 Columns 417 through 432 230 428 372 294 213 70 37 116 448 244 189 346 313 337 287 29 Columns 433 through 448 14 223 72 253 277 405 148 133 67 418 469 46 259 389 222 390 Columns 449 through 464 44 368 415 497 9 493 402 341 154 478 118 65 177 249 168 431 Columns 465 through 480 472 172 329 362 57 364 475 34 211 354 132 317 266 271 170 217 Columns 481 through 496 458 459 242 411 241 499 408 293 40 110 22 247 236 117 191 108 Columns 497 through 500 256 260 161 21 A = Columns 1 through 16 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 17 through 32 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 33 through 48 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 49 through 64 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 65 through 80 0 0 0 0 0 0 0 0 0 1 0 0 0 0 1 0 Columns 81 through 96 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 97 through 112 1 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 Columns 113 through 128 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 129 through 144 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 145 through 160 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 Columns 161 through 176 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 Columns 177 through 192 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 Columns 193 through 208 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 Columns 209 through 224 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 Columns 225 through 240 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 241 through 256 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 Columns 257 through 272 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 Columns 273 through 288 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 289 through 304 0 0 0 0 0 0 0 0 0 ...```