Cody

Problem 2356. Simulating the selection of a state with given probabilities

Solution 2809909

Submitted on 8 Aug 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
probs = rand; while sum(probs) < 1 a = rand; if a + sum(probs) > 1 probs = [probs 1-sum(probs)]; break; else probs = [probs a]; end end states = 1:length(probs); for i = 1:100 y{i,1} = select_state(probs); [nelements,centers] = hist(y{i},states); probs_result{i} = nelements/length(y{i}); error(i,1) = sum(abs(probs-probs_result{i})); end assert(mean(error) <= 0.05 & mean(error) > 0);

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

Assertion failed.