{"group":{"id":1,"name":"Community","lockable":false,"created_at":"2012-01-18T18:02:15.000Z","updated_at":"2025-12-14T01:33:56.000Z","description":"Problems submitted by members of the MATLAB Central community.","is_default":true,"created_by":161519,"badge_id":null,"featured":false,"trending":false,"solution_count_in_trending_period":0,"trending_last_calculated":"2025-12-14T00:00:00.000Z","image_id":null,"published":true,"community_created":false,"status_id":2,"is_default_group_for_player":false,"deleted_by":null,"deleted_at":null,"restored_by":null,"restored_at":null,"description_opc":null,"description_html":null,"published_at":null},"problems":[{"id":44393,"title":"Testing for randomness:  uniform distribution of integers","description":"On Cody several problems have been set up asking players to generate one or more 'random' numbers.  Usually they are asking for numbers from a uniform distribution function (UDF).  However, Test Suites do not always test very carefully to see how 'random' the sequences generated by the submitted codes are.  Indeed, rigorous testing of randomness is a sophisticated field of endeavour.  (See e.g. \u003chttps://en.wikipedia.org/wiki/TestU01 TestU01\u003e and the \u003chttps://en.wikipedia.org/wiki/Diehard_tests Diehard tests\u003e.)  \r\n\r\nMATLAB provides access to several very good pseudo-random number generators.  Even these do not generate truly random number sequences — however, they are good enough for most purposes.  \r\n\r\nHere we will be considering only integer sequences.  Your task will be to distinguish those that are 'practically' random — in this case being uniformly distributed — from those that aren't.  \r\n\r\nYou must return:\r\n\r\n* the probability of the given sequence being generated (a scalar float);  and  \r\n* your assessment of whether the sequence could 'plausibly' have been produced by a true random number generator (a scalar Boolean).  \r\n\r\nInput sequences will be vectors of various lengths containing only the integers 1, 2, 3 and 4.  \r\n\r\nHere are a few cases to consider:\r\n\r\nEXAMPLE ONE — Random sequence (UDF)\r\n\r\n Input = [3 4 3 3 4 2 2 2 1 3 2 2 1 3 3 2 4 3 1 2 3 2 1 4 2 1 4 4 4 2 3 1 2 1 2 2 1]\r\n prob ~ 5.29E-23\r\n isRandom = true\r\n\r\nEXAMPLE TWO — Non-uniform distribution\r\n\r\n Input = [1 3 3 1 1 1 1 3 1 1 4 1 2 1 1 1 1 1 3 1 3]\r\n % Notice that there are far too many ones, and too few twos and fours.\r\n prob ~ 9.09E-13\r\n isRandom = false\r\n\r\nEXAMPLE THREE — \u003chttp://www.spirit-statement.org/sequence-generation/ Blocked randomisation\u003e (or 'permuted block randomisation') \r\n\r\n Input = [1 3 2 4, 1 4 2 3, 3 1 4 2, 2 1 3 4, 1 4 3 2, 3 2 4 1, 4 1 3]\r\n % Notice that each of the four digits appears in groups of four.  In such cases the identity of neighbouring digits is often biased too, although it isn't apparent in the short sequence above.  \r\n prob ~ 5.55E-17\r\n isRandom = false\r\n\r\nEXAMPLE FOUR — Repeated pattern\r\n\r\n Input = [1 3 1 4 2 4 2 3, 1 3 1 4 2 4 2 3, 1 3 1 4 2 4 2 3, 1 3 1 4 2 4 2 3, 1 3 1 4 2]\r\n % Notice the pattern of eight digits that is repeated.  Notice also that the identity of neighbouring digits is biased, as e.g. 4 is only ever followed by 2, and 3 is only ever followed by 1.  \r\n prob ~ 5.29E-23\r\n isRandom = false\r\n\r\nEXAMPLE FIVE — Partial repeated sequence, too few runs\r\n\r\n Input = [1 2 3 4 2 3 4 1 3 4 1 2 4 1 2 3 1 2 3 1 2 4 1 3 4 2 3 4 1 2 3 4]\r\n % Notice that the sequence \"1 2 3 4\" is repeated in part or in full.  Notice also that the neighbouring digits are biased — e.g. 2 most commonly follows 1, but 2 is never followed by 1 or 2.  And finally notice that there are no runs (repeated occurrences of the same numeral).  \r\n prob ~ 5.42E-20\r\n isRandom = false\r\n\r\nEXAMPLE SIX — Partially segregated sequence, runs too long\r\n\r\n Input = [1 1 4 1 1 1 1 1 1 3 3 3 3 1 3 3 3 3 4 4 4 4 2 4 4 4 4 2 2 2 2 2 2 3 2 2]\r\n % Notice that the sequence unfolds as mostly 1, then mostly 3, then mostly 4, and mostly 2 at the end.  Notice also that the neighbouring digits are biased — each number most commonly follows itself.  And finally notice that the runs are longer than we would expect from a truly random (UDF) sequence.  \r\n prob ~ 2.12E-22\r\n isRandom = false\r\n\r\nAs will be apparent, there are various tests that _could_ be applied:  inspecting occurrence of numerals, pairs of neighbouring numerals, runs of numerals (length of runs, number of runs), repeated sequences, etc.  Your job is just to return the correct outputs.  You are *not* necessarily required to implement every test.  \r\n\r\n|Note:  the sequences in the examples above were for illustrative purposes;  most sequences in the Test Suite are considerably longer.|\r\n\r\nSee also:\r\n\r\n* \u003chttps://www.mathworks.com/matlabcentral/cody/problems/44435 Problem 44435: \"Testing for randomness: uniform distribution of real numbers (distribution checking)\"\u003e (easier)","description_html":"\u003cp\u003eOn Cody several problems have been set up asking players to generate one or more 'random' numbers.  Usually they are asking for numbers from a uniform distribution function (UDF).  However, Test Suites do not always test very carefully to see how 'random' the sequences generated by the submitted codes are.  Indeed, rigorous testing of randomness is a sophisticated field of endeavour.  (See e.g. \u003ca href = \"https://en.wikipedia.org/wiki/TestU01\"\u003eTestU01\u003c/a\u003e and the \u003ca href = \"https://en.wikipedia.org/wiki/Diehard_tests\"\u003eDiehard tests\u003c/a\u003e.)\u003c/p\u003e\u003cp\u003eMATLAB provides access to several very good pseudo-random number generators.  Even these do not generate truly random number sequences — however, they are good enough for most purposes.\u003c/p\u003e\u003cp\u003eHere we will be considering only integer sequences.  Your task will be to distinguish those that are 'practically' random — in this case being uniformly distributed — from those that aren't.\u003c/p\u003e\u003cp\u003eYou must return:\u003c/p\u003e\u003cul\u003e\u003cli\u003ethe probability of the given sequence being generated (a scalar float);  and\u003c/li\u003e\u003cli\u003eyour assessment of whether the sequence could 'plausibly' have been produced by a true random number generator (a scalar Boolean).\u003c/li\u003e\u003c/ul\u003e\u003cp\u003eInput sequences will be vectors of various lengths containing only the integers 1, 2, 3 and 4.\u003c/p\u003e\u003cp\u003eHere are a few cases to consider:\u003c/p\u003e\u003cp\u003eEXAMPLE ONE — Random sequence (UDF)\u003c/p\u003e\u003cpre\u003e Input = [3 4 3 3 4 2 2 2 1 3 2 2 1 3 3 2 4 3 1 2 3 2 1 4 2 1 4 4 4 2 3 1 2 1 2 2 1]\r\n prob ~ 5.29E-23\r\n isRandom = true\u003c/pre\u003e\u003cp\u003eEXAMPLE TWO — Non-uniform distribution\u003c/p\u003e\u003cpre\u003e Input = [1 3 3 1 1 1 1 3 1 1 4 1 2 1 1 1 1 1 3 1 3]\r\n % Notice that there are far too many ones, and too few twos and fours.\r\n prob ~ 9.09E-13\r\n isRandom = false\u003c/pre\u003e\u003cp\u003eEXAMPLE THREE — \u003ca href = \"http://www.spirit-statement.org/sequence-generation/\"\u003eBlocked randomisation\u003c/a\u003e (or 'permuted block randomisation')\u003c/p\u003e\u003cpre\u003e Input = [1 3 2 4, 1 4 2 3, 3 1 4 2, 2 1 3 4, 1 4 3 2, 3 2 4 1, 4 1 3]\r\n % Notice that each of the four digits appears in groups of four.  In such cases the identity of neighbouring digits is often biased too, although it isn't apparent in the short sequence above.  \r\n prob ~ 5.55E-17\r\n isRandom = false\u003c/pre\u003e\u003cp\u003eEXAMPLE FOUR — Repeated pattern\u003c/p\u003e\u003cpre\u003e Input = [1 3 1 4 2 4 2 3, 1 3 1 4 2 4 2 3, 1 3 1 4 2 4 2 3, 1 3 1 4 2 4 2 3, 1 3 1 4 2]\r\n % Notice the pattern of eight digits that is repeated.  Notice also that the identity of neighbouring digits is biased, as e.g. 4 is only ever followed by 2, and 3 is only ever followed by 1.  \r\n prob ~ 5.29E-23\r\n isRandom = false\u003c/pre\u003e\u003cp\u003eEXAMPLE FIVE — Partial repeated sequence, too few runs\u003c/p\u003e\u003cpre\u003e Input = [1 2 3 4 2 3 4 1 3 4 1 2 4 1 2 3 1 2 3 1 2 4 1 3 4 2 3 4 1 2 3 4]\r\n % Notice that the sequence \"1 2 3 4\" is repeated in part or in full.  Notice also that the neighbouring digits are biased — e.g. 2 most commonly follows 1, but 2 is never followed by 1 or 2.  And finally notice that there are no runs (repeated occurrences of the same numeral).  \r\n prob ~ 5.42E-20\r\n isRandom = false\u003c/pre\u003e\u003cp\u003eEXAMPLE SIX — Partially segregated sequence, runs too long\u003c/p\u003e\u003cpre\u003e Input = [1 1 4 1 1 1 1 1 1 3 3 3 3 1 3 3 3 3 4 4 4 4 2 4 4 4 4 2 2 2 2 2 2 3 2 2]\r\n % Notice that the sequence unfolds as mostly 1, then mostly 3, then mostly 4, and mostly 2 at the end.  Notice also that the neighbouring digits are biased — each number most commonly follows itself.  And finally notice that the runs are longer than we would expect from a truly random (UDF) sequence.  \r\n prob ~ 2.12E-22\r\n isRandom = false\u003c/pre\u003e\u003cp\u003eAs will be apparent, there are various tests that \u003ci\u003ecould\u003c/i\u003e be applied:  inspecting occurrence of numerals, pairs of neighbouring numerals, runs of numerals (length of runs, number of runs), repeated sequences, etc.  Your job is just to return the correct outputs.  You are \u003cb\u003enot\u003c/b\u003e necessarily required to implement every test.\u003c/p\u003e\u003cp\u003e\u003ctt\u003eNote:  the sequences in the examples above were for illustrative purposes;  most sequences in the Test Suite are considerably longer.\u003c/tt\u003e\u003c/p\u003e\u003cp\u003eSee also:\u003c/p\u003e\u003cul\u003e\u003cli\u003e\u003ca href = \"https://www.mathworks.com/matlabcentral/cody/problems/44435\"\u003eProblem 44435: \"Testing for randomness: uniform distribution of real numbers (distribution checking)\"\u003c/a\u003e (easier)\u003c/li\u003e\u003c/ul\u003e","function_template":"function [prob, isRandom] = testRandomness(x)\r\n    % Your code goes here!\r\nend","test_suite":"%%\r\n% It is possible that even a genuine random number generator could produce \r\n% a sequence that does not have the characteristics of a random number stream, \r\n% so a couple of incorrect assessments are allowed as 'leeway' in each test.  \r\n\r\n\r\n%% \r\n% Random, very short\r\nfaults = 0;\r\nisRandom_C = true;\r\nfor i = 1 : 10\r\n    x = randi(4, [1, randi(8)]);\r\n    [prob, isRandom] = testRandomness(x);\r\n%    assert( abs(prob - prob_C) / prob_C \u003c 1E-5 , 'Wrong probability.')\r\n    if ~isequal(isRandom, isRandom_C),  \r\n        faults = faults + 1;  \r\n        warning('*** Warning:  %u faults recorded so far. ***', faults);\r\n    end;\r\n    assert(faults \u003c= 2, 'Too many wrong assessments.');\r\nend;\r\n\r\n\r\n%% \r\n% Random, short\r\nfaults = 0;\r\nprob_C = 1.121038771459854E-44;\r\nisRandom_C = true;\r\nfor i = 1 : 10\r\n    x = randi(4, [1,73]);\r\n    [prob, isRandom] = testRandomness(x);\r\n    assert( abs(prob - prob_C) / prob_C \u003c 1E-5 , 'Wrong probability.')\r\n    if ~isequal(isRandom, isRandom_C),  \r\n        faults = faults + 1;  \r\n        warning('*** Warning:  %u faults recorded so far. ***', faults);\r\n    end;\r\n    assert(faults \u003c= 2, 'Too many wrong assessments.');\r\nend;\r\n\r\n\r\n%% \r\n% Random, medium\r\nfaults = 0;\r\nprob_C = 1.785917798878555E-102;\r\nisRandom_C = true;\r\nfor i = 1 : 10\r\n    x = randi(4, [1,169]);\r\n    [prob, isRandom] = testRandomness(x);\r\n    assert( abs(prob - prob_C) / prob_C \u003c 1E-5 , 'Wrong probability.')\r\n    if ~isequal(isRandom, isRandom_C),  \r\n        faults = faults + 1;  \r\n        warning('*** Warning:  %u faults recorded so far. ***', faults);\r\n    end;\r\n    assert(faults \u003c= 2, 'Too many wrong assessments.');\r\nend;\r\n\r\n\r\n%% \r\n% Random, long\r\nfaults = 0;\r\nprob_C = 9.332636185032189E-302;\r\nisRandom_C = true;\r\nfor i = 1 : 10\r\n    x = randi(4, [1,500]);\r\n    [prob, isRandom] = testRandomness(x);\r\n    assert( abs(prob - prob_C) / prob_C \u003c 1E-5 , 'Wrong probability.')\r\n    if ~isequal(isRandom, isRandom_C),  \r\n        faults = faults + 1;  \r\n        warning('*** Warning:  %u faults recorded so far. ***', faults);\r\n    end;\r\n    assert(faults \u003c= 2, 'Too many wrong assessments.');\r\nend;\r\n\r\n\r\n%% \r\n% Too many of some number, short\r\nfaults = 0;\r\nprob_C = 1.121038771459854E-44;\r\nisRandom_C = false;\r\nfor i = 1 : 10\r\n    x = randi(4, [1,73]);\r\n    x( randi(3, [1,73]) \u003c 3 ) = randi(4);\r\n    [prob, isRandom] = testRandomness(x);\r\n    assert( abs(prob - prob_C) / prob_C \u003c 1E-5 , 'Wrong probability.')\r\n    if ~isequal(isRandom, isRandom_C),  \r\n        faults = faults + 1;  \r\n        warning('*** Warning:  %u faults recorded so far. ***', faults);\r\n    end;\r\n    assert(faults \u003c= 2, 'Too many wrong assessments.');\r\nend;\r\n\r\n\r\n%% \r\n% Too many of some number, medium\r\nfaults = 0;\r\nprob_C = 1.785917798878555E-102;\r\nisRandom_C = false;\r\nfor i = 1 : 10\r\n    x = randi(4, [1,169]);\r\n    x( randi(3, [1,169]) == 1 ) = randi(4);\r\n    [prob, isRandom] = testRandomness(x);\r\n    assert( abs(prob - prob_C) / prob_C \u003c 1E-5 , 'Wrong probability.')\r\n    if ~isequal(isRandom, isRandom_C),  \r\n        faults = faults + 1;  \r\n        warning('*** Warning:  %u faults recorded so far. ***', faults);\r\n    end;\r\n    assert(faults \u003c= 2, 'Too many wrong assessments.');\r\nend;\r\n\r\n\r\n%%\r\n% Too few of some number, long\r\nfaults = 0;\r\nprob_C = 9.332636185032189E-302;\r\nisRandom_C = false;\r\nfor i = 1 : 10\r\n    x = randi(4, [1,500]);\r\n    els = find(x == randi(4));\r\n    new = randi(4, [1,length(els)]);\r\n    x(els) = new;\r\n    [prob, isRandom] = testRandomness(x);\r\n    assert( abs(prob - prob_C) / prob_C \u003c 1E-5 , 'Wrong probability.')\r\n    if ~isequal(isRandom, isRandom_C),  \r\n        faults = faults + 1;  \r\n        warning('*** Warning:  %u faults recorded so far. ***', faults);\r\n    end;\r\n    assert(faults \u003c= 2, 'Too many wrong assessments.');\r\nend;\r\n\r\n\r\n%%\r\n% Repeated patterns, long\r\nfaults = 0;\r\nprob_C = 9.332636185032189E-302;\r\nisRandom_C = false;\r\nfor i = 1 : 10\r\n    x = repmat( randi(4, [1, randi([4, 12])]) , [1,150] );\r\n    x = x(1:500);\r\n    [prob, isRandom] = testRandomness(x);\r\n    assert( abs(prob - prob_C) / prob_C \u003c 1E-5 , 'Wrong probability.')\r\n    if ~isequal(isRandom, isRandom_C),  \r\n        faults = faults + 1;  \r\n        warning('*** Warning:  %u faults recorded so far. ***', faults);\r\n    end;\r\n    assert(faults \u003c= 2, 'Too many wrong assessments.');\r\nend;\r\n\r\n\r\n%%\r\n% Blocked randomisation (i.e. permuted block randomisation), medium\r\nfaults = 0;\r\nprob_C = 1.785917798878555E-102;\r\nisRandom_C = false;\r\nfor i = 1 : 10\r\n    x = [];\r\n    for j = 1 : 50,  x = [x randperm(4)];  end;\r\n    x = x(1:169);\r\n    [prob, isRandom] = testRandomness(x);\r\n    assert( abs(prob - prob_C) / prob_C \u003c 1E-5 , 'Wrong probability.')\r\n    if ~isequal(isRandom, isRandom_C),  \r\n        faults = faults + 1;  \r\n        warning('*** Warning:  %u faults recorded so far. ***', faults);\r\n    end;\r\n    assert(faults \u003c= 2, 'Too many wrong assessments.');\r\nend;\r\n\r\n\r\n%%\r\n% Blocked randomisation (i.e. permuted block randomisation), long\r\nfaults = 0;\r\nprob_C = 9.332636185032189E-302;\r\nisRandom_C = false;\r\nfor i = 1 : 10\r\n    x = [];\r\n    for j = 1 : 125,  x = [x randperm(4)];  end;\r\n    %x = x(1:500);\r\n    [prob, isRandom] = testRandomness(x);\r\n    assert( abs(prob - prob_C) / prob_C \u003c 1E-5 , 'Wrong probability.')\r\n    if ~isequal(isRandom, isRandom_C),  \r\n        faults = faults + 1;  \r\n        warning('*** Warning:  %u faults recorded so far. ***', faults);\r\n    end;\r\n    assert(faults \u003c= 2, 'Too many wrong assessments.');\r\nend;\r\n\r\n\r\n%%\r\n% Partially repeated 4-digit sequence, long\r\nfaults = 0;\r\nprob_C = 9.332636185032189E-302;\r\nisRandom_C = false;\r\nfor i = 1 : 10\r\n    pattern = randperm(4);\r\n    x = [];\r\n    for j = 1 : 300,  \r\n        d = randi(6, [1,4]);\r\n        p = pattern;\r\n        p(d==1) = [];\r\n        x = [x p];  \r\n    end;\r\n    x = x(1:500);\r\n    [prob, isRandom] = testRandomness(x);\r\n    assert( abs(prob - prob_C) / prob_C \u003c 1E-5 , 'Wrong probability.')\r\n    if ~isequal(isRandom, isRandom_C),  \r\n        faults = faults + 1;  \r\n        warning('*** Warning:  %u faults recorded so far. ***', faults);\r\n    end;\r\n    assert(faults \u003c= 2, 'Too many wrong assessments.');\r\nend;\r\n\r\n\r\n%%\r\n% Partially repeated 8-digit sequence, long\r\nfaults = 0;\r\nprob_C = 9.332636185032189E-302;\r\nisRandom_C = false;\r\nfor i = 1 : 10\r\n    pattern = [randperm(4) randperm(4)];\r\n    x = [];\r\n    for j = 1 : 150,  \r\n        d = randi(6, [1,8]);\r\n        p = pattern;\r\n        p(d==1) = [];\r\n        x = [x p];  \r\n    end;\r\n    x = x(1:500);\r\n    [prob, isRandom] = testRandomness(x);\r\n    assert( abs(prob - prob_C) / prob_C \u003c 1E-5 , 'Wrong probability.')\r\n    if ~isequal(isRandom, isRandom_C),  \r\n        faults = faults + 1;  \r\n        warning('*** Warning:  %u faults recorded so far. ***', faults);\r\n    end;\r\n    assert(faults \u003c= 2, 'Too many wrong assessments.');\r\nend;\r\n\r\n\r\n%%\r\n% Partially segregated sequence, long\r\nfaults = 0;\r\nprob_C = 9.332636185032189E-302;\r\nisRandom_C = false;\r\nfor i = 1 : 10\r\n    pattern = [randperm(4) randperm(4)];\r\n    x = [];\r\n    for j = 1 : 50,  \r\n        p = randi(12, [1,10]);\r\n        p(p\u003e4) = randi(4);\r\n        x = [x p];  \r\n    end;\r\n    [prob, isRandom] = testRandomness(x);\r\n    assert( abs(prob - prob_C) / prob_C \u003c 1E-5 , 'Wrong probability.')\r\n    if ~isequal(isRandom, isRandom_C),  \r\n        faults = faults + 1;  \r\n        warning('*** Warning:  %u faults recorded so far. ***', faults);\r\n    end;\r\n    assert(faults \u003c= 2, 'Too many wrong assessments.');\r\nend;\r\n","published":true,"deleted":false,"likes_count":0,"comments_count":0,"created_by":64439,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":3,"test_suite_updated_at":null,"rescore_all_solutions":false,"group_id":1,"created_at":"2017-10-30T13:25:51.000Z","updated_at":"2017-12-03T13:23:19.000Z","published_at":"2017-11-01T13:29:25.000Z","restored_at":null,"restored_by":null,"spam":false,"simulink":false,"admin_reviewed":false,"description_opc":"{\"relationships\":[{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/document\",\"targetMode\":\"\",\"relationshipId\":\"rId1\",\"target\":\"/matlab/document.xml\"},{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/output\",\"targetMode\":\"\",\"relationshipId\":\"rId2\",\"target\":\"/matlab/output.xml\"}],\"parts\":[{\"partUri\":\"/matlab/document.xml\",\"relationship\":[],\"contentType\":\"application/vnd.mathworks.matlab.code.document+xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?\u003e\\n\u003cw:document xmlns:w=\\\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\\\"\u003e\u003cw:body\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eOn Cody several problems have been set up asking players to generate one or more 'random' numbers. Usually they are asking for numbers from a uniform distribution function (UDF). However, Test Suites do not always test very carefully to see how 'random' the sequences generated by the submitted codes are. Indeed, rigorous testing of randomness is a sophisticated field of endeavour. (See e.g.\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:hyperlink w:docLocation=\\\"https://en.wikipedia.org/wiki/TestU01\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eTestU01\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e and the\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:hyperlink w:docLocation=\\\"https://en.wikipedia.org/wiki/Diehard_tests\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eDiehard tests\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e.)\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eMATLAB provides access to several very good pseudo-random number generators. Even these do not generate truly random number sequences — however, they are good enough for most purposes.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eHere we will be considering only integer sequences. Your task will be to distinguish those that are 'practically' random — in this case being uniformly distributed — from those that aren't.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eYou must return:\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"ListParagraph\\\"/\u003e\u003cw:numPr\u003e\u003cw:numId w:val=\\\"1\\\"/\u003e\u003c/w:numPr\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003ethe probability of the given sequence being generated (a scalar float); and\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"ListParagraph\\\"/\u003e\u003cw:numPr\u003e\u003cw:numId w:val=\\\"1\\\"/\u003e\u003c/w:numPr\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eyour assessment of whether the sequence could 'plausibly' have been produced by a true random number generator (a scalar Boolean).\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eInput sequences will be vectors of various lengths containing only the integers 1, 2, 3 and 4.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eHere are a few cases to consider:\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eEXAMPLE ONE — Random sequence (UDF)\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[ Input = [3 4 3 3 4 2 2 2 1 3 2 2 1 3 3 2 4 3 1 2 3 2 1 4 2 1 4 4 4 2 3 1 2 1 2 2 1]\\n prob ~ 5.29E-23\\n isRandom = true]]\u003e\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eEXAMPLE TWO — Non-uniform distribution\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[ Input = [1 3 3 1 1 1 1 3 1 1 4 1 2 1 1 1 1 1 3 1 3]\\n % Notice that there are far too many ones, and too few twos and fours.\\n prob ~ 9.09E-13\\n isRandom = false]]\u003e\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eEXAMPLE THREE —\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:hyperlink w:docLocation=\\\"http://www.spirit-statement.org/sequence-generation/\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eBlocked randomisation\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e (or 'permuted block randomisation')\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[ Input = [1 3 2 4, 1 4 2 3, 3 1 4 2, 2 1 3 4, 1 4 3 2, 3 2 4 1, 4 1 3]\\n % Notice that each of the four digits appears in groups of four.  In such cases the identity of neighbouring digits is often biased too, although it isn't apparent in the short sequence above.  \\n prob ~ 5.55E-17\\n isRandom = false]]\u003e\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eEXAMPLE FOUR — Repeated pattern\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[ Input = [1 3 1 4 2 4 2 3, 1 3 1 4 2 4 2 3, 1 3 1 4 2 4 2 3, 1 3 1 4 2 4 2 3, 1 3 1 4 2]\\n % Notice the pattern of eight digits that is repeated.  Notice also that the identity of neighbouring digits is biased, as e.g. 4 is only ever followed by 2, and 3 is only ever followed by 1.  \\n prob ~ 5.29E-23\\n isRandom = false]]\u003e\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eEXAMPLE FIVE — Partial repeated sequence, too few runs\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[ Input = [1 2 3 4 2 3 4 1 3 4 1 2 4 1 2 3 1 2 3 1 2 4 1 3 4 2 3 4 1 2 3 4]\\n % Notice that the sequence \\\"1 2 3 4\\\" is repeated in part or in full.  Notice also that the neighbouring digits are biased — e.g. 2 most commonly follows 1, but 2 is never followed by 1 or 2.  And finally notice that there are no runs (repeated occurrences of the same numeral).  \\n prob ~ 5.42E-20\\n isRandom = false]]\u003e\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eEXAMPLE SIX — Partially segregated sequence, runs too long\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[ Input = [1 1 4 1 1 1 1 1 1 3 3 3 3 1 3 3 3 3 4 4 4 4 2 4 4 4 4 2 2 2 2 2 2 3 2 2]\\n % Notice that the sequence unfolds as mostly 1, then mostly 3, then mostly 4, and mostly 2 at the end.  Notice also that the neighbouring digits are biased — each number most commonly follows itself.  And finally notice that the runs are longer than we would expect from a truly random (UDF) sequence.  \\n prob ~ 2.12E-22\\n isRandom = false]]\u003e\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eAs will be apparent, there are various tests that\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:i/\u003e\u003c/w:rPr\u003e\u003cw:t\u003ecould\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e be applied: inspecting occurrence of numerals, pairs of neighbouring numerals, runs of numerals (length of runs, number of runs), repeated sequences, etc. Your job is just to return the correct outputs. You are\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003enot\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e necessarily required to implement every test.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:rFonts w:cs=\\\"monospace\\\"/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eNote: the sequences in the examples above were for illustrative purposes; most sequences in the Test Suite are considerably longer.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eSee also:\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"ListParagraph\\\"/\u003e\u003cw:numPr\u003e\u003cw:numId w:val=\\\"1\\\"/\u003e\u003c/w:numPr\u003e\u003c/w:pPr\u003e\u003cw:hyperlink w:docLocation=\\\"https://www.mathworks.com/matlabcentral/cody/problems/44435\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eProblem 44435: \\\"Testing for randomness: uniform distribution of real numbers (distribution checking)\\\"\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e (easier)\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003c/w:body\u003e\u003c/w:document\u003e\"},{\"partUri\":\"/matlab/output.xml\",\"contentType\":\"text/xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\" standalone=\\\"no\\\" ?\u003e\u003cembeddedOutputs\u003e\u003cmetaData\u003e\u003cevaluationState\u003emanual\u003c/evaluationState\u003e\u003clayoutState\u003ecode\u003c/layoutState\u003e\u003coutputStatus\u003eready\u003c/outputStatus\u003e\u003c/metaData\u003e\u003coutputArray type=\\\"array\\\"/\u003e\u003cregionArray type=\\\"array\\\"/\u003e\u003c/embeddedOutputs\u003e\"}]}"},{"id":44435,"title":"Testing for randomness: uniform distribution of real numbers (distribution checking)","description":"You will be presented with a variety of sequences of real numbers.  Your job is to answer the question:  \"Is this a uniformly distributed sequence of random numbers?\"\r\n\r\n*Only the distribution needs to be examined in this problem.*  (You do not need to check for autocorrelation.)  \r\n\r\n Input:   x, a row vector of real numbers.  \r\n Output:  y, a Boolean scalar.  \r\n\r\nSome sequences have been generated by MATLAB's (pseudo)random number generator, such as:\r\n\r\n  x = 0.062    0.945    0.006    0.301    0.638    0.920    0.043    0.533    0.539    0.292    0.218    0.465    0.826    0.172    0.686    0.040    0.489    0.3950    0.761    0.038\r\n\r\nOn average these will have a uniform distribution.  Thus, by splitting up the sequence into 'bins' we find for the example above there are twelve numbers falling into the 0.0-to-0.5 bin, and a further eight numbers falling into the 0.5-to-1.0 bin.  For this length of sequence (a total of twenty numbers), that would be a sufficiently even division to *retain* the hypothesis that the underlying distribution from which the numbers were sampled was indeed a random, uniform distribution function (UDF).  \r\nHowever, please note that it may be necessary to divide the data into more than just two bins.  \r\n\r\nOther sequences will have been constructed so as to weight unevenly.  For example:  \r\n\r\n  x = 7.004    1.002    1.984    3.868    2.048    1.014    1.066    1.337    1.074   60.266    1.008    1.783    2.196    1.065    1.0613    1.049    1.236    1.235    1.394    1.107\r\n\r\nhas fifteen numbers in the 1.0-to-2.0 bin, but then there are some weird 'outliers' in the remaining five numbers, which reach up to more than 60.  It does *not* seem likely that this would have been generated from a UDF.  \r\n\r\n|Note: the sequences in the examples above were for illustrative purposes; most sequences in the Test Suite are considerably longer.|\r\n\r\nSee also:\r\n\r\n* \u003chttps://www.mathworks.com/matlabcentral/cody/problems/44393 Problem 44393: \"Testing for randomness: uniform distribution of integers\"\u003e (harder)","description_html":"\u003cp\u003eYou will be presented with a variety of sequences of real numbers.  Your job is to answer the question:  \"Is this a uniformly distributed sequence of random numbers?\"\u003c/p\u003e\u003cp\u003e\u003cb\u003eOnly the distribution needs to be examined in this problem.\u003c/b\u003e  (You do not need to check for autocorrelation.)\u003c/p\u003e\u003cpre\u003e Input:   x, a row vector of real numbers.  \r\n Output:  y, a Boolean scalar.  \u003c/pre\u003e\u003cp\u003eSome sequences have been generated by MATLAB's (pseudo)random number generator, such as:\u003c/p\u003e\u003cpre class=\"language-matlab\"\u003ex = 0.062    0.945    0.006    0.301    0.638    0.920    0.043    0.533    0.539    0.292    0.218    0.465    0.826    0.172    0.686    0.040    0.489    0.3950    0.761    0.038\r\n\u003c/pre\u003e\u003cp\u003eOn average these will have a uniform distribution.  Thus, by splitting up the sequence into 'bins' we find for the example above there are twelve numbers falling into the 0.0-to-0.5 bin, and a further eight numbers falling into the 0.5-to-1.0 bin.  For this length of sequence (a total of twenty numbers), that would be a sufficiently even division to \u003cb\u003eretain\u003c/b\u003e the hypothesis that the underlying distribution from which the numbers were sampled was indeed a random, uniform distribution function (UDF).  \r\nHowever, please note that it may be necessary to divide the data into more than just two bins.\u003c/p\u003e\u003cp\u003eOther sequences will have been constructed so as to weight unevenly.  For example:\u003c/p\u003e\u003cpre class=\"language-matlab\"\u003ex = 7.004    1.002    1.984    3.868    2.048    1.014    1.066    1.337    1.074   60.266    1.008    1.783    2.196    1.065    1.0613    1.049    1.236    1.235    1.394    1.107\r\n\u003c/pre\u003e\u003cp\u003ehas fifteen numbers in the 1.0-to-2.0 bin, but then there are some weird 'outliers' in the remaining five numbers, which reach up to more than 60.  It does \u003cb\u003enot\u003c/b\u003e seem likely that this would have been generated from a UDF.\u003c/p\u003e\u003cp\u003e\u003ctt\u003eNote: the sequences in the examples above were for illustrative purposes; most sequences in the Test Suite are considerably longer.\u003c/tt\u003e\u003c/p\u003e\u003cp\u003eSee also:\u003c/p\u003e\u003cul\u003e\u003cli\u003e\u003ca href = \"https://www.mathworks.com/matlabcentral/cody/problems/44393\"\u003eProblem 44393: \"Testing for randomness: uniform distribution of integers\"\u003c/a\u003e (harder)\u003c/li\u003e\u003c/ul\u003e","function_template":"% Some comments could go here (or below).\r\nfunction y = isItRandom(x)\r\n    % Your code goes here!\r\n    % Some more code goes here!\r\n    % And so on  :-)\r\nend","test_suite":"% It is possible that even a genuine random number generator could produce \r\n% a sequence that does not have the characteristics of a random number stream, \r\n% so a couple of incorrect assessments are allowed as 'leeway' in each test.  \r\n\r\n%%\r\n% Random, short\r\nfaults = 0;\r\nisRandom_C = true;\r\nfor i = 1 : 10\r\n    x = 1000 * rand(1, 20+randi(10));\r\n    isRandom = isItRandom(x);\r\n    if ~isequal(isRandom, isRandom_C),  \r\n        faults = faults + 1;  \r\n        warning('*** CAUTION:  %u faults recorded so far. ***', faults);\r\n    end;\r\n    assert(faults \u003c= 2, 'Too many wrong assessments.');\r\nend;\r\n\r\n%%\r\n% Bimodal, short\r\nfaults = 0;\r\nisRandom_C = false;\r\nfor i = 1 : 10\r\n    x = [rand(1, 10+randi(5)), 999 + rand(1, 10+randi(5))];\r\n    isRandom = isItRandom(x);\r\n    if ~isequal(isRandom, isRandom_C),  \r\n        faults = faults + 1;  \r\n        warning('*** CAUTION:  %u faults recorded so far. ***', faults);\r\n    end;\r\n    assert(faults \u003c= 2, 'Too many wrong assessments.');\r\nend;\r\n\r\n\r\n%%\r\n% Random, medium\r\nfaults = 0;\r\nisRandom_C = true;\r\nfor i = 1 : 10\r\n    x = randi(1000) + 1000 * rand(1, 100+randi(10));\r\n    isRandom = isItRandom(x);\r\n    if ~isequal(isRandom, isRandom_C),  \r\n        faults = faults + 1;  \r\n        warning('*** CAUTION:  %u faults recorded so far. ***', faults);\r\n    end;\r\n    assert(faults \u003c= 2, 'Too many wrong assessments.');\r\nend;\r\n\r\n%%\r\n% Bimodal, medium\r\nfaults = 0;\r\nisRandom_C = false;\r\nfor i = 1 : 10\r\n    x = randi(1000) + [rand(1, 50+randi(5)), 999 + rand(1, 50+randi(5))];\r\n    isRandom = isItRandom(x);\r\n    if ~isequal(isRandom, isRandom_C),  \r\n        faults = faults + 1;  \r\n        warning('*** CAUTION:  %u faults recorded so far. ***', faults);\r\n    end;\r\n    assert(faults \u003c= 2, 'Too many wrong assessments.');\r\nend;\r\n\r\n%%\r\n% Skewed, medium\r\nfaults = 0;\r\nisRandom_C = false;\r\nfor i = 1 : 10\r\n    x = 10 ./ rand(1, 100+randi(10));\r\n    isRandom = isItRandom(x);\r\n    if ~isequal(isRandom, isRandom_C),  \r\n        faults = faults + 1;  \r\n        warning('*** CAUTION:  %u faults recorded so far. ***', faults);\r\n    end;\r\n    assert(faults \u003c= 2, 'Too many wrong assessments.');\r\nend;\r\n\r\n\r\n%%\r\n% Random, long\r\nfaults = 0;\r\nisRandom_C = true;\r\nfor i = 1 : 10\r\n    x = 1000 * rand(1, 500+randi(10));\r\n    isRandom = isItRandom(x);\r\n    if ~isequal(isRandom, isRandom_C),  \r\n        faults = faults + 1;  \r\n        warning('*** CAUTION:  %u faults recorded so far. ***', faults);\r\n    end;\r\n    assert(faults \u003c= 2, 'Too many wrong assessments.');\r\nend;\r\n\r\n%%\r\n% Skewed (1), long\r\nfaults = 0;\r\nisRandom_C = false;\r\nfor i = 1 : 10\r\n    x = 10 ./ rand(1, 500+randi(5)) - randi(1000);\r\n    isRandom = isItRandom(x);\r\n    if ~isequal(isRandom, isRandom_C),  \r\n        faults = faults + 1;  \r\n        warning('*** CAUTION:  %u faults recorded so far. ***', faults);\r\n    end;\r\n    assert(faults \u003c= 2, 'Too many wrong assessments.');\r\nend;\r\n\r\n%%\r\n% Skewed (2), long\r\nfaults = 0;\r\nisRandom_C = false;\r\nfor i = 1 : 10\r\n    x = randi(1000) - 10 ./ rand(1, 500+randi(5));\r\n    isRandom = isItRandom(x);\r\n    if ~isequal(isRandom, isRandom_C),  \r\n        faults = faults + 1;  \r\n        warning('*** CAUTION:  %u faults recorded so far. ***', faults);\r\n    end;\r\n    assert(faults \u003c= 2, 'Too many wrong assessments.');\r\nend;\r\n\r\n%%\r\n% Bimodal, long\r\nfaults = 0;\r\nisRandom_C = false;\r\nfor i = 1 : 10\r\n    x = [rand(1, 250+randi(5)), 999 + rand(1, 250+randi(5))];\r\n    isRandom = isItRandom(x);\r\n    if ~isequal(isRandom, isRandom_C),  \r\n        faults = faults + 1;  \r\n        warning('*** CAUTION:  %u faults recorded so far. ***', faults);\r\n    end;\r\n    assert(faults \u003c= 2, 'Too many wrong assessments.');\r\nend;\r\n\r\n%%\r\n% 'Normal' (a.k.a. 'Gaussian'), long\r\nfaults = 0;\r\nisRandom_C = false;\r\nfor i = 1 : 10\r\n    x = 250 * randn(1, 500+randi(5));\r\n    isRandom = isItRandom(x);\r\n    if ~isequal(isRandom, isRandom_C),  \r\n        faults = faults + 1;  \r\n        warning('*** CAUTION:  %u faults recorded so far. ***', faults);\r\n    end;\r\n    assert(faults \u003c= 2, 'Too many wrong assessments.');\r\nend;\r\n\r\n","published":true,"deleted":false,"likes_count":0,"comments_count":0,"created_by":64439,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":3,"test_suite_updated_at":null,"rescore_all_solutions":false,"group_id":1,"created_at":"2017-12-03T12:12:04.000Z","updated_at":"2025-11-30T03:43:10.000Z","published_at":"2017-12-03T13:21:39.000Z","restored_at":null,"restored_by":null,"spam":false,"simulink":false,"admin_reviewed":false,"description_opc":"{\"relationships\":[{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/document\",\"targetMode\":\"\",\"relationshipId\":\"rId1\",\"target\":\"/matlab/document.xml\"},{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/output\",\"targetMode\":\"\",\"relationshipId\":\"rId2\",\"target\":\"/matlab/output.xml\"}],\"parts\":[{\"partUri\":\"/matlab/document.xml\",\"relationship\":[],\"contentType\":\"application/vnd.mathworks.matlab.code.document+xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?\u003e\\n\u003cw:document xmlns:w=\\\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\\\"\u003e\u003cw:body\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eYou will be presented with a variety of sequences of real numbers. Your job is to answer the question: \\\"Is this a uniformly distributed sequence of random numbers?\\\"\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eOnly the distribution needs to be examined in this problem.\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e (You do not need to check for autocorrelation.)\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[ Input:   x, a row vector of real numbers.  \\n Output:  y, a Boolean scalar.]]\u003e\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eSome sequences have been generated by MATLAB's (pseudo)random number generator, such as:\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[x = 0.062    0.945    0.006    0.301    0.638    0.920    0.043    0.533    0.539    0.292    0.218    0.465    0.826    0.172    0.686    0.040    0.489    0.3950    0.761    0.038]]\u003e\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eOn average these will have a uniform distribution. Thus, by splitting up the sequence into 'bins' we find for the example above there are twelve numbers falling into the 0.0-to-0.5 bin, and a further eight numbers falling into the 0.5-to-1.0 bin. For this length of sequence (a total of twenty numbers), that would be a sufficiently even division to\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eretain\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e the hypothesis that the underlying distribution from which the numbers were sampled was indeed a random, uniform distribution function (UDF). However, please note that it may be necessary to divide the data into more than just two bins.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eOther sequences will have been constructed so as to weight unevenly. For example:\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[x = 7.004    1.002    1.984    3.868    2.048    1.014    1.066    1.337    1.074   60.266    1.008    1.783    2.196    1.065    1.0613    1.049    1.236    1.235    1.394    1.107]]\u003e\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003ehas fifteen numbers in the 1.0-to-2.0 bin, but then there are some weird 'outliers' in the remaining five numbers, which reach up to more than 60. It does\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003enot\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e seem likely that this would have been generated from a UDF.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:rFonts w:cs=\\\"monospace\\\"/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eNote: the sequences in the examples above were for illustrative purposes; most sequences in the Test Suite are considerably longer.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eSee also:\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"ListParagraph\\\"/\u003e\u003cw:numPr\u003e\u003cw:numId w:val=\\\"1\\\"/\u003e\u003c/w:numPr\u003e\u003c/w:pPr\u003e\u003cw:hyperlink w:docLocation=\\\"https://www.mathworks.com/matlabcentral/cody/problems/44393\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eProblem 44393: \\\"Testing for randomness: uniform distribution of integers\\\"\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e (harder)\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003c/w:body\u003e\u003c/w:document\u003e\"},{\"partUri\":\"/matlab/output.xml\",\"contentType\":\"text/xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\" standalone=\\\"no\\\" ?\u003e\u003cembeddedOutputs\u003e\u003cmetaData\u003e\u003cevaluationState\u003emanual\u003c/evaluationState\u003e\u003clayoutState\u003ecode\u003c/layoutState\u003e\u003coutputStatus\u003eready\u003c/outputStatus\u003e\u003c/metaData\u003e\u003coutputArray type=\\\"array\\\"/\u003e\u003cregionArray type=\\\"array\\\"/\u003e\u003c/embeddedOutputs\u003e\"}]}"}],"problem_search":{"errors":[],"problems":[{"id":44393,"title":"Testing for randomness:  uniform distribution of integers","description":"On Cody several problems have been set up asking players to generate one or more 'random' numbers.  Usually they are asking for numbers from a uniform distribution function (UDF).  However, Test Suites do not always test very carefully to see how 'random' the sequences generated by the submitted codes are.  Indeed, rigorous testing of randomness is a sophisticated field of endeavour.  (See e.g. \u003chttps://en.wikipedia.org/wiki/TestU01 TestU01\u003e and the \u003chttps://en.wikipedia.org/wiki/Diehard_tests Diehard tests\u003e.)  \r\n\r\nMATLAB provides access to several very good pseudo-random number generators.  Even these do not generate truly random number sequences — however, they are good enough for most purposes.  \r\n\r\nHere we will be considering only integer sequences.  Your task will be to distinguish those that are 'practically' random — in this case being uniformly distributed — from those that aren't.  \r\n\r\nYou must return:\r\n\r\n* the probability of the given sequence being generated (a scalar float);  and  \r\n* your assessment of whether the sequence could 'plausibly' have been produced by a true random number generator (a scalar Boolean).  \r\n\r\nInput sequences will be vectors of various lengths containing only the integers 1, 2, 3 and 4.  \r\n\r\nHere are a few cases to consider:\r\n\r\nEXAMPLE ONE — Random sequence (UDF)\r\n\r\n Input = [3 4 3 3 4 2 2 2 1 3 2 2 1 3 3 2 4 3 1 2 3 2 1 4 2 1 4 4 4 2 3 1 2 1 2 2 1]\r\n prob ~ 5.29E-23\r\n isRandom = true\r\n\r\nEXAMPLE TWO — Non-uniform distribution\r\n\r\n Input = [1 3 3 1 1 1 1 3 1 1 4 1 2 1 1 1 1 1 3 1 3]\r\n % Notice that there are far too many ones, and too few twos and fours.\r\n prob ~ 9.09E-13\r\n isRandom = false\r\n\r\nEXAMPLE THREE — \u003chttp://www.spirit-statement.org/sequence-generation/ Blocked randomisation\u003e (or 'permuted block randomisation') \r\n\r\n Input = [1 3 2 4, 1 4 2 3, 3 1 4 2, 2 1 3 4, 1 4 3 2, 3 2 4 1, 4 1 3]\r\n % Notice that each of the four digits appears in groups of four.  In such cases the identity of neighbouring digits is often biased too, although it isn't apparent in the short sequence above.  \r\n prob ~ 5.55E-17\r\n isRandom = false\r\n\r\nEXAMPLE FOUR — Repeated pattern\r\n\r\n Input = [1 3 1 4 2 4 2 3, 1 3 1 4 2 4 2 3, 1 3 1 4 2 4 2 3, 1 3 1 4 2 4 2 3, 1 3 1 4 2]\r\n % Notice the pattern of eight digits that is repeated.  Notice also that the identity of neighbouring digits is biased, as e.g. 4 is only ever followed by 2, and 3 is only ever followed by 1.  \r\n prob ~ 5.29E-23\r\n isRandom = false\r\n\r\nEXAMPLE FIVE — Partial repeated sequence, too few runs\r\n\r\n Input = [1 2 3 4 2 3 4 1 3 4 1 2 4 1 2 3 1 2 3 1 2 4 1 3 4 2 3 4 1 2 3 4]\r\n % Notice that the sequence \"1 2 3 4\" is repeated in part or in full.  Notice also that the neighbouring digits are biased — e.g. 2 most commonly follows 1, but 2 is never followed by 1 or 2.  And finally notice that there are no runs (repeated occurrences of the same numeral).  \r\n prob ~ 5.42E-20\r\n isRandom = false\r\n\r\nEXAMPLE SIX — Partially segregated sequence, runs too long\r\n\r\n Input = [1 1 4 1 1 1 1 1 1 3 3 3 3 1 3 3 3 3 4 4 4 4 2 4 4 4 4 2 2 2 2 2 2 3 2 2]\r\n % Notice that the sequence unfolds as mostly 1, then mostly 3, then mostly 4, and mostly 2 at the end.  Notice also that the neighbouring digits are biased — each number most commonly follows itself.  And finally notice that the runs are longer than we would expect from a truly random (UDF) sequence.  \r\n prob ~ 2.12E-22\r\n isRandom = false\r\n\r\nAs will be apparent, there are various tests that _could_ be applied:  inspecting occurrence of numerals, pairs of neighbouring numerals, runs of numerals (length of runs, number of runs), repeated sequences, etc.  Your job is just to return the correct outputs.  You are *not* necessarily required to implement every test.  \r\n\r\n|Note:  the sequences in the examples above were for illustrative purposes;  most sequences in the Test Suite are considerably longer.|\r\n\r\nSee also:\r\n\r\n* \u003chttps://www.mathworks.com/matlabcentral/cody/problems/44435 Problem 44435: \"Testing for randomness: uniform distribution of real numbers (distribution checking)\"\u003e (easier)","description_html":"\u003cp\u003eOn Cody several problems have been set up asking players to generate one or more 'random' numbers.  Usually they are asking for numbers from a uniform distribution function (UDF).  However, Test Suites do not always test very carefully to see how 'random' the sequences generated by the submitted codes are.  Indeed, rigorous testing of randomness is a sophisticated field of endeavour.  (See e.g. \u003ca href = \"https://en.wikipedia.org/wiki/TestU01\"\u003eTestU01\u003c/a\u003e and the \u003ca href = \"https://en.wikipedia.org/wiki/Diehard_tests\"\u003eDiehard tests\u003c/a\u003e.)\u003c/p\u003e\u003cp\u003eMATLAB provides access to several very good pseudo-random number generators.  Even these do not generate truly random number sequences — however, they are good enough for most purposes.\u003c/p\u003e\u003cp\u003eHere we will be considering only integer sequences.  Your task will be to distinguish those that are 'practically' random — in this case being uniformly distributed — from those that aren't.\u003c/p\u003e\u003cp\u003eYou must return:\u003c/p\u003e\u003cul\u003e\u003cli\u003ethe probability of the given sequence being generated (a scalar float);  and\u003c/li\u003e\u003cli\u003eyour assessment of whether the sequence could 'plausibly' have been produced by a true random number generator (a scalar Boolean).\u003c/li\u003e\u003c/ul\u003e\u003cp\u003eInput sequences will be vectors of various lengths containing only the integers 1, 2, 3 and 4.\u003c/p\u003e\u003cp\u003eHere are a few cases to consider:\u003c/p\u003e\u003cp\u003eEXAMPLE ONE — Random sequence (UDF)\u003c/p\u003e\u003cpre\u003e Input = [3 4 3 3 4 2 2 2 1 3 2 2 1 3 3 2 4 3 1 2 3 2 1 4 2 1 4 4 4 2 3 1 2 1 2 2 1]\r\n prob ~ 5.29E-23\r\n isRandom = true\u003c/pre\u003e\u003cp\u003eEXAMPLE TWO — Non-uniform distribution\u003c/p\u003e\u003cpre\u003e Input = [1 3 3 1 1 1 1 3 1 1 4 1 2 1 1 1 1 1 3 1 3]\r\n % Notice that there are far too many ones, and too few twos and fours.\r\n prob ~ 9.09E-13\r\n isRandom = false\u003c/pre\u003e\u003cp\u003eEXAMPLE THREE — \u003ca href = \"http://www.spirit-statement.org/sequence-generation/\"\u003eBlocked randomisation\u003c/a\u003e (or 'permuted block randomisation')\u003c/p\u003e\u003cpre\u003e Input = [1 3 2 4, 1 4 2 3, 3 1 4 2, 2 1 3 4, 1 4 3 2, 3 2 4 1, 4 1 3]\r\n % Notice that each of the four digits appears in groups of four.  In such cases the identity of neighbouring digits is often biased too, although it isn't apparent in the short sequence above.  \r\n prob ~ 5.55E-17\r\n isRandom = false\u003c/pre\u003e\u003cp\u003eEXAMPLE FOUR — Repeated pattern\u003c/p\u003e\u003cpre\u003e Input = [1 3 1 4 2 4 2 3, 1 3 1 4 2 4 2 3, 1 3 1 4 2 4 2 3, 1 3 1 4 2 4 2 3, 1 3 1 4 2]\r\n % Notice the pattern of eight digits that is repeated.  Notice also that the identity of neighbouring digits is biased, as e.g. 4 is only ever followed by 2, and 3 is only ever followed by 1.  \r\n prob ~ 5.29E-23\r\n isRandom = false\u003c/pre\u003e\u003cp\u003eEXAMPLE FIVE — Partial repeated sequence, too few runs\u003c/p\u003e\u003cpre\u003e Input = [1 2 3 4 2 3 4 1 3 4 1 2 4 1 2 3 1 2 3 1 2 4 1 3 4 2 3 4 1 2 3 4]\r\n % Notice that the sequence \"1 2 3 4\" is repeated in part or in full.  Notice also that the neighbouring digits are biased — e.g. 2 most commonly follows 1, but 2 is never followed by 1 or 2.  And finally notice that there are no runs (repeated occurrences of the same numeral).  \r\n prob ~ 5.42E-20\r\n isRandom = false\u003c/pre\u003e\u003cp\u003eEXAMPLE SIX — Partially segregated sequence, runs too long\u003c/p\u003e\u003cpre\u003e Input = [1 1 4 1 1 1 1 1 1 3 3 3 3 1 3 3 3 3 4 4 4 4 2 4 4 4 4 2 2 2 2 2 2 3 2 2]\r\n % Notice that the sequence unfolds as mostly 1, then mostly 3, then mostly 4, and mostly 2 at the end.  Notice also that the neighbouring digits are biased — each number most commonly follows itself.  And finally notice that the runs are longer than we would expect from a truly random (UDF) sequence.  \r\n prob ~ 2.12E-22\r\n isRandom = false\u003c/pre\u003e\u003cp\u003eAs will be apparent, there are various tests that \u003ci\u003ecould\u003c/i\u003e be applied:  inspecting occurrence of numerals, pairs of neighbouring numerals, runs of numerals (length of runs, number of runs), repeated sequences, etc.  Your job is just to return the correct outputs.  You are \u003cb\u003enot\u003c/b\u003e necessarily required to implement every test.\u003c/p\u003e\u003cp\u003e\u003ctt\u003eNote:  the sequences in the examples above were for illustrative purposes;  most sequences in the Test Suite are considerably longer.\u003c/tt\u003e\u003c/p\u003e\u003cp\u003eSee also:\u003c/p\u003e\u003cul\u003e\u003cli\u003e\u003ca href = \"https://www.mathworks.com/matlabcentral/cody/problems/44435\"\u003eProblem 44435: \"Testing for randomness: uniform distribution of real numbers (distribution checking)\"\u003c/a\u003e (easier)\u003c/li\u003e\u003c/ul\u003e","function_template":"function [prob, isRandom] = testRandomness(x)\r\n    % Your code goes here!\r\nend","test_suite":"%%\r\n% It is possible that even a genuine random number generator could produce \r\n% a sequence that does not have the characteristics of a random number stream, \r\n% so a couple of incorrect assessments are allowed as 'leeway' in each test.  \r\n\r\n\r\n%% \r\n% Random, very short\r\nfaults = 0;\r\nisRandom_C = true;\r\nfor i = 1 : 10\r\n    x = randi(4, [1, randi(8)]);\r\n    [prob, isRandom] = testRandomness(x);\r\n%    assert( abs(prob - prob_C) / prob_C \u003c 1E-5 , 'Wrong probability.')\r\n    if ~isequal(isRandom, isRandom_C),  \r\n        faults = faults + 1;  \r\n        warning('*** Warning:  %u faults recorded so far. ***', faults);\r\n    end;\r\n    assert(faults \u003c= 2, 'Too many wrong assessments.');\r\nend;\r\n\r\n\r\n%% \r\n% Random, short\r\nfaults = 0;\r\nprob_C = 1.121038771459854E-44;\r\nisRandom_C = true;\r\nfor i = 1 : 10\r\n    x = randi(4, [1,73]);\r\n    [prob, isRandom] = testRandomness(x);\r\n    assert( abs(prob - prob_C) / prob_C \u003c 1E-5 , 'Wrong probability.')\r\n    if ~isequal(isRandom, isRandom_C),  \r\n        faults = faults + 1;  \r\n        warning('*** Warning:  %u faults recorded so far. ***', faults);\r\n    end;\r\n    assert(faults \u003c= 2, 'Too many wrong assessments.');\r\nend;\r\n\r\n\r\n%% \r\n% Random, medium\r\nfaults = 0;\r\nprob_C = 1.785917798878555E-102;\r\nisRandom_C = true;\r\nfor i = 1 : 10\r\n    x = randi(4, [1,169]);\r\n    [prob, isRandom] = testRandomness(x);\r\n    assert( abs(prob - prob_C) / prob_C \u003c 1E-5 , 'Wrong probability.')\r\n    if ~isequal(isRandom, isRandom_C),  \r\n        faults = faults + 1;  \r\n        warning('*** Warning:  %u faults recorded so far. ***', faults);\r\n    end;\r\n    assert(faults \u003c= 2, 'Too many wrong assessments.');\r\nend;\r\n\r\n\r\n%% \r\n% Random, long\r\nfaults = 0;\r\nprob_C = 9.332636185032189E-302;\r\nisRandom_C = true;\r\nfor i = 1 : 10\r\n    x = randi(4, [1,500]);\r\n    [prob, isRandom] = testRandomness(x);\r\n    assert( abs(prob - prob_C) / prob_C \u003c 1E-5 , 'Wrong probability.')\r\n    if ~isequal(isRandom, isRandom_C),  \r\n        faults = faults + 1;  \r\n        warning('*** Warning:  %u faults recorded so far. ***', faults);\r\n    end;\r\n    assert(faults \u003c= 2, 'Too many wrong assessments.');\r\nend;\r\n\r\n\r\n%% \r\n% Too many of some number, short\r\nfaults = 0;\r\nprob_C = 1.121038771459854E-44;\r\nisRandom_C = false;\r\nfor i = 1 : 10\r\n    x = randi(4, [1,73]);\r\n    x( randi(3, [1,73]) \u003c 3 ) = randi(4);\r\n    [prob, isRandom] = testRandomness(x);\r\n    assert( abs(prob - prob_C) / prob_C \u003c 1E-5 , 'Wrong probability.')\r\n    if ~isequal(isRandom, isRandom_C),  \r\n        faults = faults + 1;  \r\n        warning('*** Warning:  %u faults recorded so far. ***', faults);\r\n    end;\r\n    assert(faults \u003c= 2, 'Too many wrong assessments.');\r\nend;\r\n\r\n\r\n%% \r\n% Too many of some number, medium\r\nfaults = 0;\r\nprob_C = 1.785917798878555E-102;\r\nisRandom_C = false;\r\nfor i = 1 : 10\r\n    x = randi(4, [1,169]);\r\n    x( randi(3, [1,169]) == 1 ) = randi(4);\r\n    [prob, isRandom] = testRandomness(x);\r\n    assert( abs(prob - prob_C) / prob_C \u003c 1E-5 , 'Wrong probability.')\r\n    if ~isequal(isRandom, isRandom_C),  \r\n        faults = faults + 1;  \r\n        warning('*** Warning:  %u faults recorded so far. ***', faults);\r\n    end;\r\n    assert(faults \u003c= 2, 'Too many wrong assessments.');\r\nend;\r\n\r\n\r\n%%\r\n% Too few of some number, long\r\nfaults = 0;\r\nprob_C = 9.332636185032189E-302;\r\nisRandom_C = false;\r\nfor i = 1 : 10\r\n    x = randi(4, [1,500]);\r\n    els = find(x == randi(4));\r\n    new = randi(4, [1,length(els)]);\r\n    x(els) = new;\r\n    [prob, isRandom] = testRandomness(x);\r\n    assert( abs(prob - prob_C) / prob_C \u003c 1E-5 , 'Wrong probability.')\r\n    if ~isequal(isRandom, isRandom_C),  \r\n        faults = faults + 1;  \r\n        warning('*** Warning:  %u faults recorded so far. ***', faults);\r\n    end;\r\n    assert(faults \u003c= 2, 'Too many wrong assessments.');\r\nend;\r\n\r\n\r\n%%\r\n% Repeated patterns, long\r\nfaults = 0;\r\nprob_C = 9.332636185032189E-302;\r\nisRandom_C = false;\r\nfor i = 1 : 10\r\n    x = repmat( randi(4, [1, randi([4, 12])]) , [1,150] );\r\n    x = x(1:500);\r\n    [prob, isRandom] = testRandomness(x);\r\n    assert( abs(prob - prob_C) / prob_C \u003c 1E-5 , 'Wrong probability.')\r\n    if ~isequal(isRandom, isRandom_C),  \r\n        faults = faults + 1;  \r\n        warning('*** Warning:  %u faults recorded so far. ***', faults);\r\n    end;\r\n    assert(faults \u003c= 2, 'Too many wrong assessments.');\r\nend;\r\n\r\n\r\n%%\r\n% Blocked randomisation (i.e. permuted block randomisation), medium\r\nfaults = 0;\r\nprob_C = 1.785917798878555E-102;\r\nisRandom_C = false;\r\nfor i = 1 : 10\r\n    x = [];\r\n    for j = 1 : 50,  x = [x randperm(4)];  end;\r\n    x = x(1:169);\r\n    [prob, isRandom] = testRandomness(x);\r\n    assert( abs(prob - prob_C) / prob_C \u003c 1E-5 , 'Wrong probability.')\r\n    if ~isequal(isRandom, isRandom_C),  \r\n        faults = faults + 1;  \r\n        warning('*** Warning:  %u faults recorded so far. ***', faults);\r\n    end;\r\n    assert(faults \u003c= 2, 'Too many wrong assessments.');\r\nend;\r\n\r\n\r\n%%\r\n% Blocked randomisation (i.e. permuted block randomisation), long\r\nfaults = 0;\r\nprob_C = 9.332636185032189E-302;\r\nisRandom_C = false;\r\nfor i = 1 : 10\r\n    x = [];\r\n    for j = 1 : 125,  x = [x randperm(4)];  end;\r\n    %x = x(1:500);\r\n    [prob, isRandom] = testRandomness(x);\r\n    assert( abs(prob - prob_C) / prob_C \u003c 1E-5 , 'Wrong probability.')\r\n    if ~isequal(isRandom, isRandom_C),  \r\n        faults = faults + 1;  \r\n        warning('*** Warning:  %u faults recorded so far. ***', faults);\r\n    end;\r\n    assert(faults \u003c= 2, 'Too many wrong assessments.');\r\nend;\r\n\r\n\r\n%%\r\n% Partially repeated 4-digit sequence, long\r\nfaults = 0;\r\nprob_C = 9.332636185032189E-302;\r\nisRandom_C = false;\r\nfor i = 1 : 10\r\n    pattern = randperm(4);\r\n    x = [];\r\n    for j = 1 : 300,  \r\n        d = randi(6, [1,4]);\r\n        p = pattern;\r\n        p(d==1) = [];\r\n        x = [x p];  \r\n    end;\r\n    x = x(1:500);\r\n    [prob, isRandom] = testRandomness(x);\r\n    assert( abs(prob - prob_C) / prob_C \u003c 1E-5 , 'Wrong probability.')\r\n    if ~isequal(isRandom, isRandom_C),  \r\n        faults = faults + 1;  \r\n        warning('*** Warning:  %u faults recorded so far. ***', faults);\r\n    end;\r\n    assert(faults \u003c= 2, 'Too many wrong assessments.');\r\nend;\r\n\r\n\r\n%%\r\n% Partially repeated 8-digit sequence, long\r\nfaults = 0;\r\nprob_C = 9.332636185032189E-302;\r\nisRandom_C = false;\r\nfor i = 1 : 10\r\n    pattern = [randperm(4) randperm(4)];\r\n    x = [];\r\n    for j = 1 : 150,  \r\n        d = randi(6, [1,8]);\r\n        p = pattern;\r\n        p(d==1) = [];\r\n        x = [x p];  \r\n    end;\r\n    x = x(1:500);\r\n    [prob, isRandom] = testRandomness(x);\r\n    assert( abs(prob - prob_C) / prob_C \u003c 1E-5 , 'Wrong probability.')\r\n    if ~isequal(isRandom, isRandom_C),  \r\n        faults = faults + 1;  \r\n        warning('*** Warning:  %u faults recorded so far. ***', faults);\r\n    end;\r\n    assert(faults \u003c= 2, 'Too many wrong assessments.');\r\nend;\r\n\r\n\r\n%%\r\n% Partially segregated sequence, long\r\nfaults = 0;\r\nprob_C = 9.332636185032189E-302;\r\nisRandom_C = false;\r\nfor i = 1 : 10\r\n    pattern = [randperm(4) randperm(4)];\r\n    x = [];\r\n    for j = 1 : 50,  \r\n        p = randi(12, [1,10]);\r\n        p(p\u003e4) = randi(4);\r\n        x = [x p];  \r\n    end;\r\n    [prob, isRandom] = testRandomness(x);\r\n    assert( abs(prob - prob_C) / prob_C \u003c 1E-5 , 'Wrong probability.')\r\n    if ~isequal(isRandom, isRandom_C),  \r\n        faults = faults + 1;  \r\n        warning('*** Warning:  %u faults recorded so far. ***', faults);\r\n    end;\r\n    assert(faults \u003c= 2, 'Too many wrong assessments.');\r\nend;\r\n","published":true,"deleted":false,"likes_count":0,"comments_count":0,"created_by":64439,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":3,"test_suite_updated_at":null,"rescore_all_solutions":false,"group_id":1,"created_at":"2017-10-30T13:25:51.000Z","updated_at":"2017-12-03T13:23:19.000Z","published_at":"2017-11-01T13:29:25.000Z","restored_at":null,"restored_by":null,"spam":false,"simulink":false,"admin_reviewed":false,"description_opc":"{\"relationships\":[{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/document\",\"targetMode\":\"\",\"relationshipId\":\"rId1\",\"target\":\"/matlab/document.xml\"},{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/output\",\"targetMode\":\"\",\"relationshipId\":\"rId2\",\"target\":\"/matlab/output.xml\"}],\"parts\":[{\"partUri\":\"/matlab/document.xml\",\"relationship\":[],\"contentType\":\"application/vnd.mathworks.matlab.code.document+xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?\u003e\\n\u003cw:document xmlns:w=\\\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\\\"\u003e\u003cw:body\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eOn Cody several problems have been set up asking players to generate one or more 'random' numbers. Usually they are asking for numbers from a uniform distribution function (UDF). However, Test Suites do not always test very carefully to see how 'random' the sequences generated by the submitted codes are. Indeed, rigorous testing of randomness is a sophisticated field of endeavour. (See e.g.\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:hyperlink w:docLocation=\\\"https://en.wikipedia.org/wiki/TestU01\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eTestU01\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e and the\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:hyperlink w:docLocation=\\\"https://en.wikipedia.org/wiki/Diehard_tests\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eDiehard tests\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e.)\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eMATLAB provides access to several very good pseudo-random number generators. Even these do not generate truly random number sequences — however, they are good enough for most purposes.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eHere we will be considering only integer sequences. Your task will be to distinguish those that are 'practically' random — in this case being uniformly distributed — from those that aren't.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eYou must return:\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"ListParagraph\\\"/\u003e\u003cw:numPr\u003e\u003cw:numId w:val=\\\"1\\\"/\u003e\u003c/w:numPr\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003ethe probability of the given sequence being generated (a scalar float); and\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"ListParagraph\\\"/\u003e\u003cw:numPr\u003e\u003cw:numId w:val=\\\"1\\\"/\u003e\u003c/w:numPr\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eyour assessment of whether the sequence could 'plausibly' have been produced by a true random number generator (a scalar Boolean).\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eInput sequences will be vectors of various lengths containing only the integers 1, 2, 3 and 4.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eHere are a few cases to consider:\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eEXAMPLE ONE — Random sequence (UDF)\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[ Input = [3 4 3 3 4 2 2 2 1 3 2 2 1 3 3 2 4 3 1 2 3 2 1 4 2 1 4 4 4 2 3 1 2 1 2 2 1]\\n prob ~ 5.29E-23\\n isRandom = true]]\u003e\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eEXAMPLE TWO — Non-uniform distribution\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[ Input = [1 3 3 1 1 1 1 3 1 1 4 1 2 1 1 1 1 1 3 1 3]\\n % Notice that there are far too many ones, and too few twos and fours.\\n prob ~ 9.09E-13\\n isRandom = false]]\u003e\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eEXAMPLE THREE —\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:hyperlink w:docLocation=\\\"http://www.spirit-statement.org/sequence-generation/\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eBlocked randomisation\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e (or 'permuted block randomisation')\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[ Input = [1 3 2 4, 1 4 2 3, 3 1 4 2, 2 1 3 4, 1 4 3 2, 3 2 4 1, 4 1 3]\\n % Notice that each of the four digits appears in groups of four.  In such cases the identity of neighbouring digits is often biased too, although it isn't apparent in the short sequence above.  \\n prob ~ 5.55E-17\\n isRandom = false]]\u003e\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eEXAMPLE FOUR — Repeated pattern\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[ Input = [1 3 1 4 2 4 2 3, 1 3 1 4 2 4 2 3, 1 3 1 4 2 4 2 3, 1 3 1 4 2 4 2 3, 1 3 1 4 2]\\n % Notice the pattern of eight digits that is repeated.  Notice also that the identity of neighbouring digits is biased, as e.g. 4 is only ever followed by 2, and 3 is only ever followed by 1.  \\n prob ~ 5.29E-23\\n isRandom = false]]\u003e\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eEXAMPLE FIVE — Partial repeated sequence, too few runs\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[ Input = [1 2 3 4 2 3 4 1 3 4 1 2 4 1 2 3 1 2 3 1 2 4 1 3 4 2 3 4 1 2 3 4]\\n % Notice that the sequence \\\"1 2 3 4\\\" is repeated in part or in full.  Notice also that the neighbouring digits are biased — e.g. 2 most commonly follows 1, but 2 is never followed by 1 or 2.  And finally notice that there are no runs (repeated occurrences of the same numeral).  \\n prob ~ 5.42E-20\\n isRandom = false]]\u003e\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eEXAMPLE SIX — Partially segregated sequence, runs too long\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[ Input = [1 1 4 1 1 1 1 1 1 3 3 3 3 1 3 3 3 3 4 4 4 4 2 4 4 4 4 2 2 2 2 2 2 3 2 2]\\n % Notice that the sequence unfolds as mostly 1, then mostly 3, then mostly 4, and mostly 2 at the end.  Notice also that the neighbouring digits are biased — each number most commonly follows itself.  And finally notice that the runs are longer than we would expect from a truly random (UDF) sequence.  \\n prob ~ 2.12E-22\\n isRandom = false]]\u003e\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eAs will be apparent, there are various tests that\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:i/\u003e\u003c/w:rPr\u003e\u003cw:t\u003ecould\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e be applied: inspecting occurrence of numerals, pairs of neighbouring numerals, runs of numerals (length of runs, number of runs), repeated sequences, etc. Your job is just to return the correct outputs. You are\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003enot\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e necessarily required to implement every test.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:rFonts w:cs=\\\"monospace\\\"/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eNote: the sequences in the examples above were for illustrative purposes; most sequences in the Test Suite are considerably longer.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eSee also:\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"ListParagraph\\\"/\u003e\u003cw:numPr\u003e\u003cw:numId w:val=\\\"1\\\"/\u003e\u003c/w:numPr\u003e\u003c/w:pPr\u003e\u003cw:hyperlink w:docLocation=\\\"https://www.mathworks.com/matlabcentral/cody/problems/44435\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eProblem 44435: \\\"Testing for randomness: uniform distribution of real numbers (distribution checking)\\\"\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e (easier)\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003c/w:body\u003e\u003c/w:document\u003e\"},{\"partUri\":\"/matlab/output.xml\",\"contentType\":\"text/xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\" standalone=\\\"no\\\" ?\u003e\u003cembeddedOutputs\u003e\u003cmetaData\u003e\u003cevaluationState\u003emanual\u003c/evaluationState\u003e\u003clayoutState\u003ecode\u003c/layoutState\u003e\u003coutputStatus\u003eready\u003c/outputStatus\u003e\u003c/metaData\u003e\u003coutputArray type=\\\"array\\\"/\u003e\u003cregionArray type=\\\"array\\\"/\u003e\u003c/embeddedOutputs\u003e\"}]}"},{"id":44435,"title":"Testing for randomness: uniform distribution of real numbers (distribution checking)","description":"You will be presented with a variety of sequences of real numbers.  Your job is to answer the question:  \"Is this a uniformly distributed sequence of random numbers?\"\r\n\r\n*Only the distribution needs to be examined in this problem.*  (You do not need to check for autocorrelation.)  \r\n\r\n Input:   x, a row vector of real numbers.  \r\n Output:  y, a Boolean scalar.  \r\n\r\nSome sequences have been generated by MATLAB's (pseudo)random number generator, such as:\r\n\r\n  x = 0.062    0.945    0.006    0.301    0.638    0.920    0.043    0.533    0.539    0.292    0.218    0.465    0.826    0.172    0.686    0.040    0.489    0.3950    0.761    0.038\r\n\r\nOn average these will have a uniform distribution.  Thus, by splitting up the sequence into 'bins' we find for the example above there are twelve numbers falling into the 0.0-to-0.5 bin, and a further eight numbers falling into the 0.5-to-1.0 bin.  For this length of sequence (a total of twenty numbers), that would be a sufficiently even division to *retain* the hypothesis that the underlying distribution from which the numbers were sampled was indeed a random, uniform distribution function (UDF).  \r\nHowever, please note that it may be necessary to divide the data into more than just two bins.  \r\n\r\nOther sequences will have been constructed so as to weight unevenly.  For example:  \r\n\r\n  x = 7.004    1.002    1.984    3.868    2.048    1.014    1.066    1.337    1.074   60.266    1.008    1.783    2.196    1.065    1.0613    1.049    1.236    1.235    1.394    1.107\r\n\r\nhas fifteen numbers in the 1.0-to-2.0 bin, but then there are some weird 'outliers' in the remaining five numbers, which reach up to more than 60.  It does *not* seem likely that this would have been generated from a UDF.  \r\n\r\n|Note: the sequences in the examples above were for illustrative purposes; most sequences in the Test Suite are considerably longer.|\r\n\r\nSee also:\r\n\r\n* \u003chttps://www.mathworks.com/matlabcentral/cody/problems/44393 Problem 44393: \"Testing for randomness: uniform distribution of integers\"\u003e (harder)","description_html":"\u003cp\u003eYou will be presented with a variety of sequences of real numbers.  Your job is to answer the question:  \"Is this a uniformly distributed sequence of random numbers?\"\u003c/p\u003e\u003cp\u003e\u003cb\u003eOnly the distribution needs to be examined in this problem.\u003c/b\u003e  (You do not need to check for autocorrelation.)\u003c/p\u003e\u003cpre\u003e Input:   x, a row vector of real numbers.  \r\n Output:  y, a Boolean scalar.  \u003c/pre\u003e\u003cp\u003eSome sequences have been generated by MATLAB's (pseudo)random number generator, such as:\u003c/p\u003e\u003cpre class=\"language-matlab\"\u003ex = 0.062    0.945    0.006    0.301    0.638    0.920    0.043    0.533    0.539    0.292    0.218    0.465    0.826    0.172    0.686    0.040    0.489    0.3950    0.761    0.038\r\n\u003c/pre\u003e\u003cp\u003eOn average these will have a uniform distribution.  Thus, by splitting up the sequence into 'bins' we find for the example above there are twelve numbers falling into the 0.0-to-0.5 bin, and a further eight numbers falling into the 0.5-to-1.0 bin.  For this length of sequence (a total of twenty numbers), that would be a sufficiently even division to \u003cb\u003eretain\u003c/b\u003e the hypothesis that the underlying distribution from which the numbers were sampled was indeed a random, uniform distribution function (UDF).  \r\nHowever, please note that it may be necessary to divide the data into more than just two bins.\u003c/p\u003e\u003cp\u003eOther sequences will have been constructed so as to weight unevenly.  For example:\u003c/p\u003e\u003cpre class=\"language-matlab\"\u003ex = 7.004    1.002    1.984    3.868    2.048    1.014    1.066    1.337    1.074   60.266    1.008    1.783    2.196    1.065    1.0613    1.049    1.236    1.235    1.394    1.107\r\n\u003c/pre\u003e\u003cp\u003ehas fifteen numbers in the 1.0-to-2.0 bin, but then there are some weird 'outliers' in the remaining five numbers, which reach up to more than 60.  It does \u003cb\u003enot\u003c/b\u003e seem likely that this would have been generated from a UDF.\u003c/p\u003e\u003cp\u003e\u003ctt\u003eNote: the sequences in the examples above were for illustrative purposes; most sequences in the Test Suite are considerably longer.\u003c/tt\u003e\u003c/p\u003e\u003cp\u003eSee also:\u003c/p\u003e\u003cul\u003e\u003cli\u003e\u003ca href = \"https://www.mathworks.com/matlabcentral/cody/problems/44393\"\u003eProblem 44393: \"Testing for randomness: uniform distribution of integers\"\u003c/a\u003e (harder)\u003c/li\u003e\u003c/ul\u003e","function_template":"% Some comments could go here (or below).\r\nfunction y = isItRandom(x)\r\n    % Your code goes here!\r\n    % Some more code goes here!\r\n    % And so on  :-)\r\nend","test_suite":"% It is possible that even a genuine random number generator could produce \r\n% a sequence that does not have the characteristics of a random number stream, \r\n% so a couple of incorrect assessments are allowed as 'leeway' in each test.  \r\n\r\n%%\r\n% Random, short\r\nfaults = 0;\r\nisRandom_C = true;\r\nfor i = 1 : 10\r\n    x = 1000 * rand(1, 20+randi(10));\r\n    isRandom = isItRandom(x);\r\n    if ~isequal(isRandom, isRandom_C),  \r\n        faults = faults + 1;  \r\n        warning('*** CAUTION:  %u faults recorded so far. ***', faults);\r\n    end;\r\n    assert(faults \u003c= 2, 'Too many wrong assessments.');\r\nend;\r\n\r\n%%\r\n% Bimodal, short\r\nfaults = 0;\r\nisRandom_C = false;\r\nfor i = 1 : 10\r\n    x = [rand(1, 10+randi(5)), 999 + rand(1, 10+randi(5))];\r\n    isRandom = isItRandom(x);\r\n    if ~isequal(isRandom, isRandom_C),  \r\n        faults = faults + 1;  \r\n        warning('*** CAUTION:  %u faults recorded so far. ***', faults);\r\n    end;\r\n    assert(faults \u003c= 2, 'Too many wrong assessments.');\r\nend;\r\n\r\n\r\n%%\r\n% Random, medium\r\nfaults = 0;\r\nisRandom_C = true;\r\nfor i = 1 : 10\r\n    x = randi(1000) + 1000 * rand(1, 100+randi(10));\r\n    isRandom = isItRandom(x);\r\n    if ~isequal(isRandom, isRandom_C),  \r\n        faults = faults + 1;  \r\n        warning('*** CAUTION:  %u faults recorded so far. ***', faults);\r\n    end;\r\n    assert(faults \u003c= 2, 'Too many wrong assessments.');\r\nend;\r\n\r\n%%\r\n% Bimodal, medium\r\nfaults = 0;\r\nisRandom_C = false;\r\nfor i = 1 : 10\r\n    x = randi(1000) + [rand(1, 50+randi(5)), 999 + rand(1, 50+randi(5))];\r\n    isRandom = isItRandom(x);\r\n    if ~isequal(isRandom, isRandom_C),  \r\n        faults = faults + 1;  \r\n        warning('*** CAUTION:  %u faults recorded so far. ***', faults);\r\n    end;\r\n    assert(faults \u003c= 2, 'Too many wrong assessments.');\r\nend;\r\n\r\n%%\r\n% Skewed, medium\r\nfaults = 0;\r\nisRandom_C = false;\r\nfor i = 1 : 10\r\n    x = 10 ./ rand(1, 100+randi(10));\r\n    isRandom = isItRandom(x);\r\n    if ~isequal(isRandom, isRandom_C),  \r\n        faults = faults + 1;  \r\n        warning('*** CAUTION:  %u faults recorded so far. ***', faults);\r\n    end;\r\n    assert(faults \u003c= 2, 'Too many wrong assessments.');\r\nend;\r\n\r\n\r\n%%\r\n% Random, long\r\nfaults = 0;\r\nisRandom_C = true;\r\nfor i = 1 : 10\r\n    x = 1000 * rand(1, 500+randi(10));\r\n    isRandom = isItRandom(x);\r\n    if ~isequal(isRandom, isRandom_C),  \r\n        faults = faults + 1;  \r\n        warning('*** CAUTION:  %u faults recorded so far. ***', faults);\r\n    end;\r\n    assert(faults \u003c= 2, 'Too many wrong assessments.');\r\nend;\r\n\r\n%%\r\n% Skewed (1), long\r\nfaults = 0;\r\nisRandom_C = false;\r\nfor i = 1 : 10\r\n    x = 10 ./ rand(1, 500+randi(5)) - randi(1000);\r\n    isRandom = isItRandom(x);\r\n    if ~isequal(isRandom, isRandom_C),  \r\n        faults = faults + 1;  \r\n        warning('*** CAUTION:  %u faults recorded so far. ***', faults);\r\n    end;\r\n    assert(faults \u003c= 2, 'Too many wrong assessments.');\r\nend;\r\n\r\n%%\r\n% Skewed (2), long\r\nfaults = 0;\r\nisRandom_C = false;\r\nfor i = 1 : 10\r\n    x = randi(1000) - 10 ./ rand(1, 500+randi(5));\r\n    isRandom = isItRandom(x);\r\n    if ~isequal(isRandom, isRandom_C),  \r\n        faults = faults + 1;  \r\n        warning('*** CAUTION:  %u faults recorded so far. ***', faults);\r\n    end;\r\n    assert(faults \u003c= 2, 'Too many wrong assessments.');\r\nend;\r\n\r\n%%\r\n% Bimodal, long\r\nfaults = 0;\r\nisRandom_C = false;\r\nfor i = 1 : 10\r\n    x = [rand(1, 250+randi(5)), 999 + rand(1, 250+randi(5))];\r\n    isRandom = isItRandom(x);\r\n    if ~isequal(isRandom, isRandom_C),  \r\n        faults = faults + 1;  \r\n        warning('*** CAUTION:  %u faults recorded so far. ***', faults);\r\n    end;\r\n    assert(faults \u003c= 2, 'Too many wrong assessments.');\r\nend;\r\n\r\n%%\r\n% 'Normal' (a.k.a. 'Gaussian'), long\r\nfaults = 0;\r\nisRandom_C = false;\r\nfor i = 1 : 10\r\n    x = 250 * randn(1, 500+randi(5));\r\n    isRandom = isItRandom(x);\r\n    if ~isequal(isRandom, isRandom_C),  \r\n        faults = faults + 1;  \r\n        warning('*** CAUTION:  %u faults recorded so far. ***', faults);\r\n    end;\r\n    assert(faults \u003c= 2, 'Too many wrong assessments.');\r\nend;\r\n\r\n","published":true,"deleted":false,"likes_count":0,"comments_count":0,"created_by":64439,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":3,"test_suite_updated_at":null,"rescore_all_solutions":false,"group_id":1,"created_at":"2017-12-03T12:12:04.000Z","updated_at":"2025-11-30T03:43:10.000Z","published_at":"2017-12-03T13:21:39.000Z","restored_at":null,"restored_by":null,"spam":false,"simulink":false,"admin_reviewed":false,"description_opc":"{\"relationships\":[{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/document\",\"targetMode\":\"\",\"relationshipId\":\"rId1\",\"target\":\"/matlab/document.xml\"},{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/output\",\"targetMode\":\"\",\"relationshipId\":\"rId2\",\"target\":\"/matlab/output.xml\"}],\"parts\":[{\"partUri\":\"/matlab/document.xml\",\"relationship\":[],\"contentType\":\"application/vnd.mathworks.matlab.code.document+xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?\u003e\\n\u003cw:document xmlns:w=\\\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\\\"\u003e\u003cw:body\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eYou will be presented with a variety of sequences of real numbers. Your job is to answer the question: \\\"Is this a uniformly distributed sequence of random numbers?\\\"\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eOnly the distribution needs to be examined in this problem.\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e (You do not need to check for autocorrelation.)\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[ Input:   x, a row vector of real numbers.  \\n Output:  y, a Boolean scalar.]]\u003e\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eSome sequences have been generated by MATLAB's (pseudo)random number generator, such as:\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[x = 0.062    0.945    0.006    0.301    0.638    0.920    0.043    0.533    0.539    0.292    0.218    0.465    0.826    0.172    0.686    0.040    0.489    0.3950    0.761    0.038]]\u003e\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eOn average these will have a uniform distribution. Thus, by splitting up the sequence into 'bins' we find for the example above there are twelve numbers falling into the 0.0-to-0.5 bin, and a further eight numbers falling into the 0.5-to-1.0 bin. For this length of sequence (a total of twenty numbers), that would be a sufficiently even division to\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eretain\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e the hypothesis that the underlying distribution from which the numbers were sampled was indeed a random, uniform distribution function (UDF). However, please note that it may be necessary to divide the data into more than just two bins.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eOther sequences will have been constructed so as to weight unevenly. For example:\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[x = 7.004    1.002    1.984    3.868    2.048    1.014    1.066    1.337    1.074   60.266    1.008    1.783    2.196    1.065    1.0613    1.049    1.236    1.235    1.394    1.107]]\u003e\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003ehas fifteen numbers in the 1.0-to-2.0 bin, but then there are some weird 'outliers' in the remaining five numbers, which reach up to more than 60. It does\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003enot\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e seem likely that this would have been generated from a UDF.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:rFonts w:cs=\\\"monospace\\\"/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eNote: the sequences in the examples above were for illustrative purposes; most sequences in the Test Suite are considerably longer.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eSee also:\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"ListParagraph\\\"/\u003e\u003cw:numPr\u003e\u003cw:numId w:val=\\\"1\\\"/\u003e\u003c/w:numPr\u003e\u003c/w:pPr\u003e\u003cw:hyperlink w:docLocation=\\\"https://www.mathworks.com/matlabcentral/cody/problems/44393\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eProblem 44393: \\\"Testing for randomness: uniform distribution of integers\\\"\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e (harder)\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003c/w:body\u003e\u003c/w:document\u003e\"},{\"partUri\":\"/matlab/output.xml\",\"contentType\":\"text/xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\" standalone=\\\"no\\\" ?\u003e\u003cembeddedOutputs\u003e\u003cmetaData\u003e\u003cevaluationState\u003emanual\u003c/evaluationState\u003e\u003clayoutState\u003ecode\u003c/layoutState\u003e\u003coutputStatus\u003eready\u003c/outputStatus\u003e\u003c/metaData\u003e\u003coutputArray type=\\\"array\\\"/\u003e\u003cregionArray type=\\\"array\\\"/\u003e\u003c/embeddedOutputs\u003e\"}]}"}],"term":"tag:\"randomness\"","current_player_id":null,"fields":[{"name":"page","type":"integer","callback":null,"default":1,"directive":null,"facet":null,"facet_method":"and","operator":null,"param":null,"static":null,"prepend":true},{"name":"per_page","type":"integer","callback":null,"default":50,"directive":null,"facet":null,"facet_method":"and","operator":null,"param":null,"static":null,"prepend":true},{"name":"sort","type":"string","callback":null,"default":null,"directive":null,"facet":null,"facet_method":"and","operator":null,"param":null,"static":null,"prepend":true},{"name":"body","type":"text","callback":null,"default":"*:*","directive":null,"facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":false},{"name":"group","type":"string","callback":null,"default":null,"directive":"group","facet":true,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"difficulty_rating_bin","type":"string","callback":null,"default":null,"directive":"difficulty_rating_bin","facet":true,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"id","type":"integer","callback":null,"default":null,"directive":"id","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"tag","type":"string","callback":null,"default":null,"directive":"tag","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"product","type":"string","callback":null,"default":null,"directive":"product","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"created_at","type":"timeframe","callback":{},"default":null,"directive":"created_at","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"profile_id","type":"integer","callback":null,"default":null,"directive":"author_id","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"created_by","type":"string","callback":null,"default":null,"directive":"author","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"player_id","type":"integer","callback":null,"default":null,"directive":"solver_id","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"player","type":"string","callback":null,"default":null,"directive":"solver","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"solvers_count","type":"integer","callback":null,"default":null,"directive":"solvers_count","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"comments_count","type":"integer","callback":null,"default":null,"directive":"comments_count","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"likes_count","type":"integer","callback":null,"default":null,"directive":"likes_count","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"leader_id","type":"integer","callback":null,"default":null,"directive":"leader_id","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"leading_solution","type":"integer","callback":null,"default":null,"directive":"leading_solution","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true}],"filters":[{"name":"asset_type","type":"string","callback":null,"default":null,"directive":null,"facet":null,"facet_method":"and","operator":null,"param":null,"static":"\"cody:problem\"","prepend":true},{"name":"profile_id","type":"integer","callback":{},"default":null,"directive":null,"facet":null,"facet_method":"and","operator":null,"param":"author_id","static":null,"prepend":true}],"query":{"params":{"per_page":50,"term":"tag:\"randomness\"","current_player":null,"sort":"map(difficulty_value,0,0,999) asc"},"parser":"MathWorks::Search::Solr::QueryParser","directives":{"term":{"directives":{"tag":[["tag:\"randomness\"","","\"","randomness","\""]]}}},"facets":{"#\u003cMathWorks::Search::Field:0x00007f1943cf8ad8\u003e":null,"#\u003cMathWorks::Search::Field:0x00007f1943cf8a38\u003e":null},"filters":{"#\u003cMathWorks::Search::Field:0x00007f1943cf8178\u003e":"\"cody:problem\""},"fields":{"#\u003cMathWorks::Search::Field:0x00007f1943cf8d58\u003e":1,"#\u003cMathWorks::Search::Field:0x00007f1943cf8cb8\u003e":50,"#\u003cMathWorks::Search::Field:0x00007f1943cf8c18\u003e":"map(difficulty_value,0,0,999) asc","#\u003cMathWorks::Search::Field:0x00007f1943cf8b78\u003e":"tag:\"randomness\""},"user_query":{"#\u003cMathWorks::Search::Field:0x00007f1943cf8b78\u003e":"tag:\"randomness\""},"queried_facets":{}},"query_backend":{"connection":{"configuration":{"index_url":"http://index-op-v2/solr/","query_url":"http://search-op-v2/solr/","direct_access_index_urls":["http://index-op-v2/solr/"],"direct_access_query_urls":["http://search-op-v2/solr/"],"timeout":10,"vhost":"search","exchange":"search.topic","heartbeat":30,"pre_index_mode":false,"host":"rabbitmq-eks","port":5672,"username":"search","password":"J3bGPZzQ7asjJcCk","virtual_host":"search","indexer":"amqp","http_logging":"true","core":"cody"},"query_connection":{"uri":"http://search-op-v2/solr/cody/","proxy":null,"connection":{"parallel_manager":null,"headers":{"User-Agent":"Faraday v1.0.1"},"params":{},"options":{"params_encoder":"Faraday::FlatParamsEncoder","proxy":null,"bind":null,"timeout":null,"open_timeout":null,"read_timeout":null,"write_timeout":null,"boundary":null,"oauth":null,"context":null,"on_data":null},"ssl":{"verify":true,"ca_file":null,"ca_path":null,"verify_mode":null,"cert_store":null,"client_cert":null,"client_key":null,"certificate":null,"private_key":null,"verify_depth":null,"version":null,"min_version":null,"max_version":null},"default_parallel_manager":null,"builder":{"adapter":{"name":"Faraday::Adapter::NetHttp","args":[],"block":null},"handlers":[{"name":"Faraday::Response::RaiseError","args":[],"block":null}],"app":{"app":{"ssl_cert_store":{"verify_callback":null,"error":null,"error_string":null,"chain":null,"time":null},"app":{},"connection_options":{},"config_block":null}}},"url_prefix":"http://search-op-v2/solr/cody/","manual_proxy":false,"proxy":null},"update_format":"RSolr::JSON::Generator","update_path":"update","options":{"url":"http://search-op-v2/solr/cody"}}},"query":{"params":{"per_page":50,"term":"tag:\"randomness\"","current_player":null,"sort":"map(difficulty_value,0,0,999) asc"},"parser":"MathWorks::Search::Solr::QueryParser","directives":{"term":{"directives":{"tag":[["tag:\"randomness\"","","\"","randomness","\""]]}}},"facets":{"#\u003cMathWorks::Search::Field:0x00007f1943cf8ad8\u003e":null,"#\u003cMathWorks::Search::Field:0x00007f1943cf8a38\u003e":null},"filters":{"#\u003cMathWorks::Search::Field:0x00007f1943cf8178\u003e":"\"cody:problem\""},"fields":{"#\u003cMathWorks::Search::Field:0x00007f1943cf8d58\u003e":1,"#\u003cMathWorks::Search::Field:0x00007f1943cf8cb8\u003e":50,"#\u003cMathWorks::Search::Field:0x00007f1943cf8c18\u003e":"map(difficulty_value,0,0,999) asc","#\u003cMathWorks::Search::Field:0x00007f1943cf8b78\u003e":"tag:\"randomness\""},"user_query":{"#\u003cMathWorks::Search::Field:0x00007f1943cf8b78\u003e":"tag:\"randomness\""},"queried_facets":{}},"options":{"fields":["id","difficulty_rating"]},"join":" "},"results":[{"id":44393,"difficulty_rating":"medium"},{"id":44435,"difficulty_rating":"hard"}]}}