Cody

# Problem 1089. Create a random vector of integers with given sum

Solution 196548

Submitted on 25 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
%% nocheat = isempty(regexp(evalc('type random_sequence'),'([^f]eval|regexprep|inline|str2func)')); m = 26; s = 5000; y = random_sequence(m,s); assert(isequal(sum(y),s) && abs(mean(y)-m/2)<m*sqrt(m/s)+1/2 && isequal(y,round(y)) && abs(std(y)-m/sqrt(12))*sqrt(s)/m<2.5 && nocheat)

y2 = Columns 1 through 8 5 29 55 67 70 77 88 104 Columns 9 through 16 111 127 146 152 156 164 173 185 Columns 17 through 24 199 202 209 230 231 256 275 288 Columns 25 through 32 304 311 323 349 364 378 385 398 Columns 33 through 40 415 433 444 454 480 481 505 529 Columns 41 through 48 550 553 560 569 587 591 610 613 Columns 49 through 56 630 643 664 683 707 731 740 759 Columns 57 through 64 765 766 786 800 813 837 853 870 Columns 65 through 72 893 914 929 934 941 965 966 979 Columns 73 through 80 984 1010 1029 1043 1056 1058 1076 1078 Columns 81 through 88 1080 1094 1097 1119 1141 1160 1164 1182 Columns 89 through 96 1196 1222 1239 1260 1272 1284 1306 1309 Columns 97 through 104 1313 1318 1329 1351 1372 1374 1385 1399 Columns 105 through 112 1410 1428 1445 1453 1465 1466 1492 1497 Columns 113 through 120 1500 1510 1516 1529 1538 1563 1587 1589 Columns 121 through 128 1609 1616 1627 1642 1667 1678 1704 1712 Columns 129 through 136 1731 1749 1764 1783 1801 1806 1810 1836 Columns 137 through 144 1841 1842 1857 1880 1898 1903 1913 1925 Columns 145 through 152 1951 1956 1979 1996 2006 2011 2023 2036 Columns 153 through 160 2040 2056 2062 2073 2089 2096 2104 2121 Columns 161 through 168 2128 2150 2176 2195 2204 2220 2223 2247 Columns 169 through 176 2270 2292 2299 2315 2316 2328 2337 2342 Columns 177 through 184 2347 2358 2361 2377 2390 2409 2428 2445 Columns 185 through 192 2446 2448 2457 2471 2489 2500 2522 2541 Columns 193 through 200 2567 2581 2590 2593 2609 2630 2642 2645 Columns 201 through 208 2652 2656 2664 2676 2690 2702 2725 2739 Columns 209 through 216 2764 2781 2806 2813 2831 2839 2857 2876 Columns 217 through 224 2878 2885 2891 2909 2931 2940 2961 2979 Columns 225 through 232 2980 2996 3007 3031 3032 3045 3057 3069 Columns 233 through 240 3090 3099 3120 3133 3134 3139 3158 3171 Columns 241 through 248 3175 3184 3200 3205 3225 3232 3256 3263 Columns 249 through 256 3283 3288 3296 3299 3314 3332 3347 3359 Columns 257 through 264 3376 3393 3411 3428 3453 3459 3478 3485 Columns 265 through 272 3489 3505 3517 3529 3547 3568 3578 3596 Columns 273 through 280 3607 3629 3651 3658 3674 3690 3705 3728 Columns 281 through 288 3735 3744 3748 3773 3790 3803 3820 3835 Columns 289 through 296 3852 3867 3886 3900 3926 3932 3935 3938 Columns 297 through 304 3940 3951 3963 3973 3993 4010 4031 4056 Columns 305 through 312 4082 4087 4091 4110 4113 4127 4141 4164 Columns 313 through 320 4177 4188 4206 4226 4240 4250 4254 4270 Columns 321 through 328 4277 4279 4299 4306 4318 4336 4346 4366 Columns 329 through 336 4377 4395 4414 4426 4427 4436 4448 4456 Columns 337 through 344 4462 4484 4496 4520 4531 4551 4562 4584 Columns 345 through 352 4604 4614 4620 4641 4666 4675 4693 4705 Columns 353 through 360 4727 4747 4752 4775 4801 4815 4838 4854 Columns 361 through 368 4859 4865 4876 4896 4918 4939 4948 4962 Columns 369 through 376 4965 4968 4972 4990 5003 5008 5021 5025 Columns 377 through 384 5027 5050 5065 5090 5109 5125 5147 5170 Columns 385 through 392 5196 5197 5220 5236 5262 5276 5289 5310 Columns 393 through 400 5316 5329 5353 5368 5390 5410 5426 5433 Columns 401 through 408 5451 5454 5471 5489 5508 5532 5558 5578 Columns 409 through 416 5594 5619 5635 5636 5640 5663 5676 5698 Columns 417 through 424 5704 5719 5736 5737 5753 5763 5765 5778 Columns 425 through 432 5784 5788 5794 5798 5803 5805 5822 5830 Columns 433 through 440 5845 5864 5877 5891 5903 5907 5920 5943 Columns 441 through 448 5966 5974 5980 5995 6012 6023 6029 6054 Columns 449 through 456 6057 6060 6064 6069 6086 6101 6103 6128 Columns 457 through 464 6147 6167 6169 6192 6217 6243 6266 6287 Columns 465 through 472 6301 6306 6317 6321 6322 6347 6355 6363 Columns 473 through 480 6372 6385 6402 6403 6425 6440 6463 6473 Columns 481 through 488 6485 6487 6492 6510 6519 6543 6547 6573 Columns 489 through 496 6588 6607 6633 6641 6652 6665 6685 6707 Columns 497 through 504 6710 6715 6725 6727 6741 6750 6755 6761 Columns 505 through 512 6785 6803 6816 6840 6843 6863 6883 6898 Columns 513 through 520 6903 6919 6927 6931 6937 6961 6963 6970 Columns 521 through 528 6972 6984 6985 7009 7015 7018 7026 7038 Columns 529 through 536 7041 7067 7076 7084 7086 7094 7096 7110 Columns 537 through 544 7130 7147 7150 7153 7174 7198 7212 7215 Columns 545 through 552 7237 7246 7254 7274 7275 7277 7295 7311 Columns 553 through 560 7325 7344 7363 7384 7392 7411 7426 7437 Columns 561 through 568 7439 7460 7469 7485 7505 7508 7512 7527 Columns 569 through 576 7540 7564 7585 7605 7607 7609 7612 7633 Columns 577 through 584 7658 7676 7680 7699 7702 7706 7723 7732 Columns 585 through 592 7749 7769 7785 7805 7812 7832 7858 7881 Columns 593 through 600 7884 7894 7904 7922 7938 7959 7969 7975 Columns 601 through 608 7978 7999 8005 8016 8031 8037 8054 8067 Columns 609 through 616 8071 8092 8095 8103 8110 8124 8127 8138 Columns 617 through 624 8141 8144 8165 8173 8189 8215 8227 8246 Columns 625 through 632 8266 8278 8296 8299 8324 8329 8336 8357 Columns 633 through 640 8370 8390 8401 8409 8410 8428 8440 8452 Columns 641 through 648 8468 8470 8479 8500 8519 8523 8527 8530 Columns 649 through 656 8531 8543 8...

2   Pass
%% nocheat = isempty(regexp(evalc('type random_sequence'),'([^f]eval|regexprep|inline|str2func)')); m = 2; s = 1000; y = random_sequence(m,s); assert(isequal(sum(y),s) && abs(mean(y)-m/2)<m*sqrt(m/s)+1/2 && isequal(y,round(y)) && abs(std(y)-m/sqrt(12))*sqrt(s)/m<2.5 && nocheat)

y2 = Columns 1 through 8 2 4 6 8 10 12 14 15 Columns 9 through 16 16 18 19 21 22 23 24 26 Columns 17 through 24 28 30 32 34 36 38 39 40 Columns 25 through 32 41 43 45 46 48 49 50 52 Columns 33 through 40 54 56 58 60 62 63 64 66 Columns 41 through 48 67 68 69 71 73 75 77 79 Columns 49 through 56 81 82 84 85 86 87 89 91 Columns 57 through 64 93 94 96 98 99 100 102 104 Columns 65 through 72 105 106 107 109 110 112 114 115 Columns 73 through 80 117 119 121 123 124 125 126 127 Columns 81 through 88 128 129 131 132 134 136 137 138 Columns 89 through 96 139 141 142 144 145 146 148 150 Columns 97 through 104 152 153 154 156 158 159 160 161 Columns 105 through 112 163 164 165 166 167 168 169 170 Columns 113 through 120 171 173 174 176 177 178 180 182 Columns 121 through 128 183 184 185 187 189 191 192 194 Columns 129 through 136 196 197 199 200 202 204 206 207 Columns 137 through 144 209 210 212 214 216 217 218 219 Columns 145 through 152 221 223 224 226 227 228 229 231 Columns 153 through 160 233 235 236 237 238 239 241 242 Columns 161 through 168 243 245 246 248 250 252 253 255 Columns 169 through 176 257 259 260 262 263 265 267 268 Columns 177 through 184 270 271 273 274 276 278 280 281 Columns 185 through 192 282 284 286 287 288 290 292 293 Columns 193 through 200 295 297 299 300 301 302 304 305 Columns 201 through 208 307 308 309 311 313 315 316 317 Columns 209 through 216 318 320 322 324 326 328 330 331 Columns 217 through 224 332 334 336 337 338 339 340 341 Columns 225 through 232 343 344 345 347 348 349 350 351 Columns 233 through 240 352 354 355 357 359 361 363 365 Columns 241 through 248 367 368 370 371 372 373 374 375 Columns 249 through 256 377 378 380 382 384 385 386 387 Columns 257 through 264 389 390 391 392 394 396 398 399 Columns 265 through 272 401 402 403 405 406 408 410 411 Columns 273 through 280 412 413 414 416 417 419 420 422 Columns 281 through 288 423 424 425 426 427 429 430 431 Columns 289 through 296 433 434 435 436 437 439 440 442 Columns 297 through 304 444 445 446 448 449 450 451 453 Columns 305 through 312 455 456 458 459 460 462 463 464 Columns 313 through 320 465 467 468 470 471 472 474 475 Columns 321 through 328 477 479 481 483 484 485 486 488 Columns 329 through 336 489 491 492 493 495 497 499 500 Columns 337 through 344 501 502 504 506 508 510 512 513 Columns 345 through 352 515 517 519 521 522 524 525 527 Columns 353 through 360 529 530 532 533 535 537 539 540 Columns 361 through 368 541 543 545 546 548 549 551 553 Columns 369 through 376 554 555 557 558 559 560 561 563 Columns 377 through 384 564 566 567 568 569 570 572 573 Columns 385 through 392 575 576 577 579 580 582 584 585 Columns 393 through 400 586 587 589 590 591 593 595 596 Columns 401 through 408 598 599 601 603 605 606 607 609 Columns 409 through 416 611 612 613 614 615 616 617 618 Columns 417 through 424 620 622 624 625 627 628 629 631 Columns 425 through 432 633 634 635 636 637 638 639 640 Columns 433 through 440 642 644 645 647 648 650 652 653 Columns 441 through 448 654 655 657 658 660 662 664 665 Columns 449 through 456 666 667 669 670 671 673 675 676 Columns 457 through 464 678 679 680 682 684 686 687 688 Columns 465 through 472 690 692 693 695 696 697 699 700 Columns 473 through 480 702 703 704 705 706 708 710 712 Columns 481 through 488 714 716 718 719 720 721 722 724 Columns 489 through 496 725 727 728 730 732 733 735 736 Columns 497 through 504 737 739 740 741 743 744 746 748 Columns 505 through 512 749 751 753 754 756 758 760 762 Columns 513 through 520 763 765 766 768 770 771 773 775 Columns 521 through 528 776 777 779 781 783 784 785 787 Columns 529 through 536 789 790 792 794 796 797 798 799 Columns 537 through 544 800 801 803 804 805 807 809 810 Columns 545 through 552 812 814 816 817 818 819 820 821 Columns 553 through 560 822 824 825 826 828 829 830 832 Columns 561 through 568 834 835 837 838 840 842 843 844 Columns 569 through 576 845 846 847 849 851 852 854 855 Columns 577 through 584 857 859 861 862 864 865 866 867 Columns 585 through 592 869 870 871 872 874 876 878 880 Columns 593 through 600 881 882 884 885 887 889 890 891 Columns 601 through 608 892 894 896 897 899 900 902 904 Columns 609 through 616 905 906 908 910 912 913 914 916 Columns 617 through 624 918 919 921 923 924 925 926 927 Columns 625 through 632 928 930 931 933 935 936 938 940 Columns 633 through 640 941 943 944 946 947 948 950 952 Columns 641 through 648 954 956 957 959 960 961 962 964 Columns 649 through 656 966 968 ...

3   Fail
%% nocheat = isempty(regexp(evalc('type random_sequence'),'([^f]eval|regexprep|inline|str2func)')); m = 1000; s = 100000; y = random_sequence(m,s); assert(isequal(sum(y),s) && abs(mean(y)-m/2)<m*sqrt(m/s)+1/2 && isequal(y,round(y)) && abs(std(y)-m/sqrt(12))*sqrt(s^1/m^3)<1 && nocheat)

y2 = Columns 1 through 8 176 593 1333 2226 2252 2390 2815 3580 Columns 9 through 16 4105 4860 5030 5703 6322 6329 7070 8062 Columns 17 through 24 8191 8552 8744 9459 9637 10624 10641 10681 Columns 25 through 32 11486 12346 12913 13668 14188 14774 14965 15469 Columns 33 through 40 15520 15577 15913 16544 17436 18110 18796 19492 Columns 41 through 48 20292 20953 21473 21805 22740 22987 23499 24240 Columns 49 through 56 24495 25341 25880 26791 27142 28078 29006 29594 Columns 57 through 64 29696 30063 30339 30606 30833 30835 31729 32183 Columns 65 through 72 32762 33078 34073 35057 36022 36689 37416 37751 Columns 73 through 80 38274 38548 39267 40046 40128 40350 40554 41179 Columns 81 through 88 41905 42740 42759 42962 43432 43811 44152 44216 Columns 89 through 96 44978 45381 46056 46608 46660 46968 47934 48866 Columns 97 through 104 49245 49863 50426 51257 52215 52291 53180 53743 Columns 105 through 112 53938 54160 54866 55463 56050 57019 57601 57701 Columns 113 through 120 57868 57971 58118 58790 59430 59803 59966 60356 Columns 121 through 128 61156 61556 62312 62608 63249 64135 64345 64826 Columns 129 through 136 64939 65072 65137 65217 65844 66256 66895 67752 Columns 137 through 144 68516 69493 70275 71211 71951 72205 72925 73619 Columns 145 through 152 74393 75141 75460 75971 76745 77318 78272 78444 Columns 153 through 160 79352 80105 80392 81020 81483 81613 82163 83133 Columns 161 through 168 83576 84167 84278 84478 84641 84678 84951 85182 Columns 169 through 176 85541 85676 86675 87189 87577 87827 88192 88592 Columns 177 through 184 89519 90015 90625 90630 91218 91996 92654 93183 Columns 185 through 192 94009 94972 95286 96084 96370 96385 96480 96809 Columns 193 through 200 97115 97134 97297 97741 98508 99190 99905 100365 Columns 201 through 208 101285 102274 103207 103669 104574 104961 105565 106126 Columns 209 through 216 106972 107257 107921 108524 109181 109491 109823 110012 Columns 217 through 224 110113 110400 110755 111291 112282 112311 113021 113927 Columns 225 through 232 114793 114913 115869 116310 117186 118051 118407 119039 Columns 233 through 240 119904 119925 120002 120379 120529 120564 121347 121675 Columns 241 through 248 122493 122667 123344 124220 124978 125208 125567 125931 Columns 249 through 256 126199 126537 126624 127076 127531 127561 128199 128259 Columns 257 through 264 128429 129114 129669 129676 129965 130342 130489 130564 Columns 265 through 272 131025 131396 132221 132759 133577 134038 134053 134058 Columns 273 through 280 134225 134591 135310 135470 135749 136394 136682 137004 Columns 281 through 288 137160 137548 138444 139332 139726 140402 140655 141605 Columns 289 through 296 142230 142437 142547 143113 143388 143460 143619 143669 Columns 297 through 304 144350 145132 145940 146206 147102 147710 147725 148070 Columns 305 through 312 148604 149232 149679 150491 150636 151611 152445 152786 Columns 313 through 320 153403 153707 153797 154319 155145 155909 156857 157191 Columns 321 through 328 157581 157732 158066 158620 159171 159332 159450 159849 Columns 329 through 336 160681 160867 161368 161495 162360 163127 163692 164082 Columns 337 through 344 164573 165460 166366 166865 167395 168305 168884 169660 Columns 345 through 352 170322 170792 171012 171615 171800 171998 172860 172986 Columns 353 through 360 173632 174070 174683 175421 175725 175768 176604 176974 Columns 361 through 368 177636 178532 178807 179805 180640 181432 182089 182633 Columns 369 through 376 183020 183843 184439 185221 186209 186998 187851 188337 Columns 377 through 384 189211 189545 189751 190246 190278 191106 191371 192049 Columns 385 through 392 192843 193525 194176 194414 194892 195829 196071 196281 Columns 393 through 400 196554 197330 197662 198266 198451 198539 198848 199079 Columns 401 through 408 199989 200926 200958 201552 201596 202021 202543 203384 Columns 409 through 416 204010 204266 205171 205939 206502 207400 207769 208107 Columns 417 through 424 208726 208813 209189 209377 210188 210912 211587 212501 Columns 425 through 432 213322 213871 213887 213982 214497 215450 216294 217230 Columns 433 through 440 218204 218577 218898 219682 219895 220855 221786 222152 Columns 441 through 448 222497 223190 223907 224708 225302 225400 225960 226004 Columns 449 through 456 226762 227422 228022 228249 228963 229688 230033 230066 Columns 457 through 464 230910 231681 232191 232898 233690 234021 234935 235512 Columns 465 through 472 235800 236067 236958 237325 237476 238423 239074 240057 Columns 473 through 480 240251 241080 241574 242165 242210 242775 243409 243908 Columns 481 through 488 243968 244484 244538 245382 245977 246669 247317 247941 Columns 489 through 496 248895 249072 249547 250390 250887 251650 251830 252522 Columns 497 through 504 253437 253600 254029 254071 254297 254964 255836 256181 Columns 505 through 512 256209 257115 257587 258498 258826 259518 259536 260049 Columns 513 through 520 260381 260718 261672 262423 262757 263390 263806 264383 Columns 521 through 528 265343 265959 266110 266949 267676 267785

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!