Cody

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

Solution 2156632

Submitted on 11 Mar 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
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)

Error using randperm Size inputs must be scalar. Error in random_sequence (line 4) randperm([ones(1,abs(d)) zeros(1,numel(ans)-abs(d))])*(2*(d>0)-1)+ans; Error in Test1 (line 4) y = random_sequence(m,s);

2   Fail
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)

Error using randperm Size inputs must be scalar. Error in random_sequence (line 4) randperm([ones(1,abs(d)) zeros(1,numel(ans)-abs(d))])*(2*(d>0)-1)+ans; Error in Test2 (line 4) y = random_sequence(m,s);

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)

Error using randperm Size inputs must be scalar. Error in random_sequence (line 4) randperm([ones(1,abs(d)) zeros(1,numel(ans)-abs(d))])*(2*(d>0)-1)+ans; Error in Test3 (line 4) y = random_sequence(m,s);

### Community Treasure Hunt

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

Start Hunting!