Cody

# Problem 44393. Testing for randomness: uniform distribution of integers

Solution 1683552

Submitted on 30 Nov 2018 by Binbin Qi
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
% It is possible that even a genuine random number generator could produce % a sequence that does not have the characteristics of a random number stream, % so a couple of incorrect assessments are allowed as 'leeway' in each test.

2   Pass
% Random, very short faults = 0; isRandom_C = true; for i = 1 : 10 x = randi(4, [1, randi(8)]); [prob, isRandom] = testRandomness(x); % assert( abs(prob - prob_C) / prob_C < 1E-5 , 'Wrong probability.') if ~isequal(isRandom, isRandom_C), faults = faults + 1; warning('*** Warning: %u faults recorded so far. ***', faults); end; assert(faults <= 2, 'Too many wrong assessments.'); end;

3   Pass
% Random, short faults = 0; prob_C = 1.121038771459854E-44; isRandom_C = true; for i = 1 : 10 x = randi(4, [1,73]); [prob, isRandom] = testRandomness(x); assert( abs(prob - prob_C) / prob_C < 1E-5 , 'Wrong probability.') if ~isequal(isRandom, isRandom_C), faults = faults + 1; warning('*** Warning: %u faults recorded so far. ***', faults); end; assert(faults <= 2, 'Too many wrong assessments.'); end;

[Warning: *** Warning: 1 faults recorded so far. ***] [> In ScoringEngineTestPoint3 (line 11) In solutionTest (line 7)]

4   Pass
% Random, medium faults = 0; prob_C = 1.785917798878555E-102; isRandom_C = true; for i = 1 : 10 x = randi(4, [1,169]); [prob, isRandom] = testRandomness(x); assert( abs(prob - prob_C) / prob_C < 1E-5 , 'Wrong probability.') if ~isequal(isRandom, isRandom_C), faults = faults + 1; warning('*** Warning: %u faults recorded so far. ***', faults); end; assert(faults <= 2, 'Too many wrong assessments.'); end;

5   Pass
% Random, long faults = 0; prob_C = 9.332636185032189E-302; isRandom_C = true; for i = 1 : 10 x = randi(4, [1,500]); [prob, isRandom] = testRandomness(x); assert( abs(prob - prob_C) / prob_C < 1E-5 , 'Wrong probability.') if ~isequal(isRandom, isRandom_C), faults = faults + 1; warning('*** Warning: %u faults recorded so far. ***', faults); end; assert(faults <= 2, 'Too many wrong assessments.'); end;

[Warning: *** Warning: 1 faults recorded so far. ***] [> In ScoringEngineTestPoint5 (line 11) In solutionTest (line 11)]

6   Pass
% Too many of some number, short faults = 0; prob_C = 1.121038771459854E-44; isRandom_C = false; for i = 1 : 10 x = randi(4, [1,73]); x( randi(3, [1,73]) < 3 ) = randi(4); [prob, isRandom] = testRandomness(x); assert( abs(prob - prob_C) / prob_C < 1E-5 , 'Wrong probability.') if ~isequal(isRandom, isRandom_C), faults = faults + 1; warning('*** Warning: %u faults recorded so far. ***', faults); end; assert(faults <= 2, 'Too many wrong assessments.'); end;

7   Pass
% Too many of some number, medium faults = 0; prob_C = 1.785917798878555E-102; isRandom_C = false; for i = 1 : 10 x = randi(4, [1,169]); x( randi(3, [1,169]) == 1 ) = randi(4); [prob, isRandom] = testRandomness(x); assert( abs(prob - prob_C) / prob_C < 1E-5 , 'Wrong probability.') if ~isequal(isRandom, isRandom_C), faults = faults + 1; warning('*** Warning: %u faults recorded so far. ***', faults); end; assert(faults <= 2, 'Too many wrong assessments.'); end;

8   Pass
% Too few of some number, long faults = 0; prob_C = 9.332636185032189E-302; isRandom_C = false; for i = 1 : 10 x = randi(4, [1,500]); els = find(x == randi(4)); new = randi(4, [1,length(els)]); x(els) = new; [prob, isRandom] = testRandomness(x); assert( abs(prob - prob_C) / prob_C < 1E-5 , 'Wrong probability.') if ~isequal(isRandom, isRandom_C), faults = faults + 1; warning('*** Warning: %u faults recorded so far. ***', faults); end; assert(faults <= 2, 'Too many wrong assessments.'); end;

9   Pass
% Repeated patterns, long faults = 0; prob_C = 9.332636185032189E-302; isRandom_C = false; for i = 1 : 10 x = repmat( randi(4, [1, randi([4, 12])]) , [1,150] ); x = x(1:500); [prob, isRandom] = testRandomness(x); assert( abs(prob - prob_C) / prob_C < 1E-5 , 'Wrong probability.') if ~isequal(isRandom, isRandom_C), faults = faults + 1; warning('*** Warning: %u faults recorded so far. ***', faults); end; assert(faults <= 2, 'Too many wrong assessments.'); end;

10   Pass
% Blocked randomisation (i.e. permuted block randomisation), medium faults = 0; prob_C = 1.785917798878555E-102; isRandom_C = false; for i = 1 : 10 x = []; for j = 1 : 50, x = [x randperm(4)]; end; x = x(1:169); [prob, isRandom] = testRandomness(x); assert( abs(prob - prob_C) / prob_C < 1E-5 , 'Wrong probability.') if ~isequal(isRandom, isRandom_C), faults = faults + 1; warning('*** Warning: %u faults recorded so far. ***', faults); end; assert(faults <= 2, 'Too many wrong assessments.'); end;

11   Pass
% Blocked randomisation (i.e. permuted block randomisation), long faults = 0; prob_C = 9.332636185032189E-302; isRandom_C = false; for i = 1 : 10 x = []; for j = 1 : 125, x = [x randperm(4)]; end; %x = x(1:500); [prob, isRandom] = testRandomness(x); assert( abs(prob - prob_C) / prob_C < 1E-5 , 'Wrong probability.') if ~isequal(isRandom, isRandom_C), faults = faults + 1; warning('*** Warning: %u faults recorded so far. ***', faults); end; assert(faults <= 2, 'Too many wrong assessments.'); end;

12   Pass
% Partially repeated 4-digit sequence, long faults = 0; prob_C = 9.332636185032189E-302; isRandom_C = false; for i = 1 : 10 pattern = randperm(4); x = []; for j = 1 : 300, d = randi(6, [1,4]); p = pattern; p(d==1) = []; x = [x p]; end; x = x(1:500); [prob, isRandom] = testRandomness(x); assert( abs(prob - prob_C) / prob_C < 1E-5 , 'Wrong probability.') if ~isequal(isRandom, isRandom_C), faults = faults + 1; warning('*** Warning: %u faults recorded so far. ***', faults); end; assert(faults <= 2, 'Too many wrong assessments.'); end;

13   Pass
% Partially repeated 8-digit sequence, long faults = 0; prob_C = 9.332636185032189E-302; isRandom_C = false; for i = 1 : 10 pattern = [randperm(4) randperm(4)]; x = []; for j = 1 : 150, d = randi(6, [1,8]); p = pattern; p(d==1) = []; x = [x p]; end; x = x(1:500); [prob, isRandom] = testRandomness(x); assert( abs(prob - prob_C) / prob_C < 1E-5 , 'Wrong probability.') if ~isequal(isRandom, isRandom_C), faults = faults + 1; warning('*** Warning: %u faults recorded so far. ***', faults); end; assert(faults <= 2, 'Too many wrong assessments.'); end;

14   Pass
% Partially segregated sequence, long faults = 0; prob_C = 9.332636185032189E-302; isRandom_C = false; for i = 1 : 10 pattern = [randperm(4) randperm(4)]; x = []; for j = 1 : 50, p = randi(12, [1,10]); p(p>4) = randi(4); x = [x p]; end; [prob, isRandom] = testRandomness(x); assert( abs(prob - prob_C) / prob_C < 1E-5 , 'Wrong probability.') if ~isequal(isRandom, isRandom_C), faults = faults + 1; warning('*** Warning: %u faults recorded so far. ***', faults); end; assert(faults <= 2, 'Too many wrong assessments.'); end;