{"group":{"id":1,"name":"Community","lockable":false,"created_at":"2012-01-18T18:02:15.000Z","updated_at":"2026-04-06T14:01:22.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":"2026-04-06T00: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":44630,"title":"Guess the number I'm thinking of (Part 1)","description":"In this game you are competing against two other people to guess the number that I'm thinking of.\r\nI randomly choose an integer between one and ten (inclusive). I don't provide any clues about the number.\r\nYour first opponent tries to guess the number. They guess randomly.\r\nYour second opponent tries to guess the number. They also guess randomly.\r\nYou try to guess the number. But you guess strategically.\r\nThe winner is the person who guesses my chosen number, or the person who guesses closest to my chosen number. This represents a \"win\".\r\nIf two contestants are equally close, they may share the win, with such a result being declared a \"draw\". (It is a loss for the remaining contestant.) A draw is worth half as much as a win.\r\nEach person hears the guesses stated by any preceding competitors, so you will be aware of the two prior guesses (provided to you as the vector guessesOfOpponents). Moreover, each guess must be unique.\r\nIf everyone guessed randomly, each person should have an equal chance of winning.\r\nIt might seem that you're at a disadvantage, having the last opportunity to guess. But actually you have the advantage of extra knowledge.\r\nBy guessing strategically, you should be able to achieve a success rate of 45% or more, in which\r\nsuccess rate = (wins + draws/2) / games\r\n\r\nRELATED PROBLEM:  \r\nProblem 52323. Guess the number I'm thinking of (Part 2)","description_html":"\u003cdiv style = \"text-align: start; line-height: 20.44px; min-height: 0px; white-space: normal; color: rgb(0, 0, 0); font-family: Menlo, Monaco, Consolas, monospace; font-style: normal; font-size: 14px; font-weight: 400; text-decoration: none solid rgb(0, 0, 0); white-space: normal; \"\u003e\u003cdiv style=\"block-size: 484px; display: block; min-width: 0px; padding-block-start: 0px; padding-top: 0px; perspective-origin: 407px 242px; transform-origin: 407px 242px; vertical-align: baseline; \"\u003e\u003cdiv style=\"block-size: 21px; font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-block-end: 9px; margin-block-start: 2px; margin-bottom: 9px; margin-inline-end: 10px; margin-inline-start: 4px; margin-left: 4px; margin-right: 10px; margin-top: 2px; perspective-origin: 384px 10.5px; text-align: left; transform-origin: 384px 10.5px; white-space: pre-wrap; margin-left: 4px; margin-top: 2px; margin-bottom: 9px; margin-right: 10px; \"\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003eIn this game you are competing against two other people to guess the number that I'm thinking of.\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003col style=\"block-size: 160px; font-family: Helvetica, Arial, sans-serif; list-style-type: decimal; margin-block-end: 20px; margin-block-start: 10px; margin-bottom: 20px; margin-top: 10px; perspective-origin: 391px 80px; transform-origin: 391px 80px; margin-top: 10px; margin-bottom: 20px; \"\u003e\u003cli style=\"block-size: 20px; display: list-item; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-start: 56px; margin-left: 56px; margin-top: 0px; perspective-origin: 363px 10px; text-align: left; transform-origin: 363px 10px; white-space: pre-wrap; margin-left: 56px; \"\u003e\u003cspan style=\"block-size: auto; display: inline; margin-inline-start: 0px; margin-left: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003eI randomly choose an\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"block-size: auto; display: inline; margin-inline-start: 0px; margin-left: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003e \u003c/span\u003e\u003c/span\u003e\u003cspan style=\"block-size: auto; display: inline; margin-inline-start: 0px; margin-left: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; \"\u003e\u003cspan style=\"font-weight: 700; \"\u003einteger\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"block-size: auto; display: inline; margin-inline-start: 0px; margin-left: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003e between\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"block-size: auto; display: inline; margin-inline-start: 0px; margin-left: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003e \u003c/span\u003e\u003c/span\u003e\u003cspan style=\"block-size: auto; display: inline; margin-inline-start: 0px; margin-left: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; \"\u003e\u003cspan style=\"font-weight: 700; \"\u003eone\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"block-size: auto; display: inline; margin-inline-start: 0px; margin-left: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003e and\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"block-size: auto; display: inline; margin-inline-start: 0px; margin-left: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003e \u003c/span\u003e\u003c/span\u003e\u003cspan style=\"block-size: auto; display: inline; margin-inline-start: 0px; margin-left: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; \"\u003e\u003cspan style=\"font-weight: 700; \"\u003eten\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"block-size: auto; display: inline; margin-inline-start: 0px; margin-left: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003e (inclusive). I don't provide any clues about the number.\u003c/span\u003e\u003c/span\u003e\u003c/li\u003e\u003cli style=\"block-size: 20px; display: list-item; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-start: 56px; margin-left: 56px; margin-top: 0px; perspective-origin: 363px 10px; text-align: left; transform-origin: 363px 10px; white-space: pre-wrap; margin-left: 56px; \"\u003e\u003cspan style=\"block-size: auto; display: inline; margin-inline-start: 0px; margin-left: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003eYour first opponent tries to guess the number. They guess randomly.\u003c/span\u003e\u003c/span\u003e\u003c/li\u003e\u003cli style=\"block-size: 20px; display: list-item; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-start: 56px; margin-left: 56px; margin-top: 0px; perspective-origin: 363px 10px; text-align: left; transform-origin: 363px 10px; white-space: pre-wrap; margin-left: 56px; \"\u003e\u003cspan style=\"block-size: auto; display: inline; margin-inline-start: 0px; margin-left: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003eYour second opponent tries to guess the number. They also guess randomly.\u003c/span\u003e\u003c/span\u003e\u003c/li\u003e\u003cli style=\"block-size: 20px; display: list-item; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-start: 56px; margin-left: 56px; margin-top: 0px; perspective-origin: 363px 10px; text-align: left; transform-origin: 363px 10px; white-space: pre-wrap; margin-left: 56px; \"\u003e\u003cspan style=\"block-size: auto; display: inline; margin-inline-start: 0px; margin-left: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003eYou try to guess the number. But you guess\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"block-size: auto; display: inline; margin-inline-start: 0px; margin-left: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003e \u003c/span\u003e\u003c/span\u003e\u003cspan style=\"block-size: auto; display: inline; margin-inline-start: 0px; margin-left: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; \"\u003e\u003cspan style=\"font-style: italic; \"\u003estrategically\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"block-size: auto; display: inline; margin-inline-start: 0px; margin-left: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003e.\u003c/span\u003e\u003c/span\u003e\u003c/li\u003e\u003cli style=\"block-size: 40px; display: list-item; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-start: 56px; margin-left: 56px; margin-top: 0px; perspective-origin: 363px 20px; text-align: left; transform-origin: 363px 20px; white-space: pre-wrap; margin-left: 56px; \"\u003e\u003cspan style=\"block-size: auto; display: inline; margin-inline-start: 0px; margin-left: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003eThe winner is the person who guesses my chosen number, or the person who guesses closest to my chosen number. This represents a \"win\".\u003c/span\u003e\u003c/span\u003e\u003c/li\u003e\u003cli style=\"block-size: 40px; display: list-item; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-start: 56px; margin-left: 56px; margin-top: 0px; perspective-origin: 363px 20px; text-align: left; transform-origin: 363px 20px; white-space: pre-wrap; margin-left: 56px; \"\u003e\u003cspan style=\"block-size: auto; display: inline; margin-inline-start: 0px; margin-left: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003eIf two contestants are equally close, they may share the win, with such a result being declared a \"draw\". (It is a loss for the remaining contestant.) A draw is worth half as much as a win.\u003c/span\u003e\u003c/span\u003e\u003c/li\u003e\u003c/ol\u003e\u003cdiv style=\"block-size: 42px; font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-block-end: 9px; margin-block-start: 2px; margin-bottom: 9px; margin-inline-end: 10px; margin-inline-start: 4px; margin-left: 4px; margin-right: 10px; margin-top: 2px; perspective-origin: 384px 21px; text-align: left; transform-origin: 384px 21px; white-space: pre-wrap; margin-left: 4px; margin-top: 2px; margin-bottom: 9px; margin-right: 10px; \"\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003eEach person hears the guesses stated by any preceding competitors, so you will be aware of the two prior guesses (provided to you as the vector\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003e \u003c/span\u003e\u003c/span\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; \"\u003e\u003cspan style=\"font-family: Menlo, Monaco, Consolas, \u0026quot;Courier New\u0026quot;, monospace; \"\u003eguessesOfOpponents\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003e). Moreover, each guess must be unique.\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"block-size: 21px; font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-block-end: 9px; margin-block-start: 2px; margin-bottom: 9px; margin-inline-end: 10px; margin-inline-start: 4px; margin-left: 4px; margin-right: 10px; margin-top: 2px; perspective-origin: 384px 10.5px; text-align: left; transform-origin: 384px 10.5px; white-space: pre-wrap; margin-left: 4px; margin-top: 2px; margin-bottom: 9px; margin-right: 10px; \"\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003eIf everyone guessed randomly, each person should have an equal chance of winning.\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"block-size: 42px; font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-block-end: 9px; margin-block-start: 2px; margin-bottom: 9px; margin-inline-end: 10px; margin-inline-start: 4px; margin-left: 4px; margin-right: 10px; margin-top: 2px; perspective-origin: 384px 21px; text-align: left; transform-origin: 384px 21px; white-space: pre-wrap; margin-left: 4px; margin-top: 2px; margin-bottom: 9px; margin-right: 10px; \"\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003eIt might seem that you're at a disadvantage, having the last opportunity to guess. But actually you have the advantage of extra knowledge.\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"block-size: 21px; font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-block-end: 9px; margin-block-start: 2px; margin-bottom: 9px; margin-inline-end: 10px; margin-inline-start: 4px; margin-left: 4px; margin-right: 10px; margin-top: 2px; perspective-origin: 384px 10.5px; text-align: left; transform-origin: 384px 10.5px; white-space: pre-wrap; margin-left: 4px; margin-top: 2px; margin-bottom: 9px; margin-right: 10px; \"\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003eBy guessing strategically, you should be able to\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003e \u003c/span\u003e\u003c/span\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; \"\u003e\u003cspan style=\"font-weight: 700; \"\u003eachieve a success rate of 45% or more\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003e, in which\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"block-size: 21px; font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-block-end: 9px; margin-block-start: 2px; margin-bottom: 9px; margin-inline-end: 10px; margin-inline-start: 4px; margin-left: 4px; margin-right: 10px; margin-top: 2px; perspective-origin: 384px 10.5px; text-align: left; transform-origin: 384px 10.5px; white-space: pre-wrap; margin-left: 4px; margin-top: 2px; margin-bottom: 9px; margin-right: 10px; \"\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003esuccess rate = (wins + draws/2) / games\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"block-size: 21px; font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-block-end: 9px; margin-block-start: 2px; margin-bottom: 9px; margin-inline-end: 10px; margin-inline-start: 4px; margin-left: 4px; margin-right: 10px; margin-top: 2px; perspective-origin: 384px 10.5px; text-align: left; transform-origin: 384px 10.5px; white-space: pre-wrap; margin-left: 4px; margin-top: 2px; margin-bottom: 9px; margin-right: 10px; \"\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003e\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"block-size: 21px; font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-block-end: 9px; margin-block-start: 2px; margin-bottom: 9px; margin-inline-end: 10px; margin-inline-start: 4px; margin-left: 4px; margin-right: 10px; margin-top: 2px; perspective-origin: 384px 10.5px; text-align: left; transform-origin: 384px 10.5px; white-space: pre-wrap; margin-left: 4px; margin-top: 2px; margin-bottom: 9px; margin-right: 10px; \"\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003eRELATED PROBLEM:  \u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cul style=\"block-size: 20px; font-family: Helvetica, Arial, sans-serif; list-style-type: square; margin-block-end: 20px; margin-block-start: 10px; margin-bottom: 20px; margin-top: 10px; perspective-origin: 391px 10px; transform-origin: 391px 10px; margin-top: 10px; margin-bottom: 20px; \"\u003e\u003cli style=\"display: list-item; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-start: 56px; margin-left: 56px; margin-top: 0px; perspective-origin: 363px 10px; text-align: left; transform-origin: 363px 10px; white-space: pre-wrap; margin-left: 56px; \"\u003e\u003ca target='_blank' href = \"https://www.mathworks.com/matlabcentral/cody/problems/52323\"\u003e\u003cspan style=\"\"\u003e\u003cspan style=\"\"\u003eProblem 52323. Guess the number I'm thinking of (Part 2)\u003c/span\u003e\u003c/span\u003e\u003c/a\u003e\u003c/li\u003e\u003c/ul\u003e\u003c/div\u003e\u003c/div\u003e","function_template":"function y = myGuess(guessesOfOpponents)\r\n    y = 42;\r\nend","test_suite":"%% Anti-hacking test\r\nassessFunctionAbsence({'rng', 'RandStream'}, 'FileName','myGuess.m')\r\n\r\n%% Ensure unique guesses of integers, which are in-range\r\nfor j = 1 : 1000\r\n    numberToBeGuessed = randi(10);\r\n    gOO = randperm(10, 2);\r\n    mG = myGuess(gOO);\r\n    assert( mG \u003e= 1  \u0026  mG \u003c= 10 , 'Out of requested range.' )\r\n    u = unique( floor([gOO mG]) );\r\n    assert( length(u) == 3 , 'Your guess must not have been already chosen.' )\r\nend;\r\n\r\n%% maxIts: 20000 = Too small; 30000 = Not quite big enough; 35000 = Just big enough (usually!); 50000 = Big enough (usually!); 100000 = Big enough, plus safety margin \u0026 efficiency incentive (but waste of resources)\r\nmaxIts = 100000;    \r\ntic\r\nfor j = 1 : 10\r\n    WDL = [0 0 0];\r\n    for itn = 1 : maxIts\r\n        numberToBeGuessed = randi(10);\r\n        gOO = randperm(10, 2);\r\n        diffs = abs( [gOO myGuess(gOO)] - numberToBeGuessed );\r\n        winningContestant = find( min(diffs)==diffs );\r\n        if any( winningContestant == 3 ),\r\n            if length(winningContestant) == 1,\r\n                % Win\r\n                WDL(1) = WDL(1) + 1;  \r\n            else\r\n                % Draw\r\n                WDL(2) = WDL(2) + 1;  \r\n            end;\r\n        else\r\n            % Loss\r\n            WDL(3) = WDL(3) + 1;  \r\n        end;\r\n    end;\r\n    successRate = (WDL(1) + WDL(2)/2) / maxIts\r\n    assert( successRate \u003e= 0.45 )\r\nend;\r\ntoc","published":true,"deleted":false,"likes_count":13,"comments_count":6,"created_by":64439,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":69,"test_suite_updated_at":null,"rescore_all_solutions":false,"group_id":1,"created_at":"2018-05-04T14:00:17.000Z","updated_at":"2026-02-06T20:26:39.000Z","published_at":"2018-05-05T12:29:22.000Z","restored_at":null,"restored_by":null,"spam":false,"simulink":false,"admin_reviewed":false,"description_opc":"{\"parts\":[{\"partUri\":\"/matlab/document.xml\",\"contentType\":\"application/vnd.mathworks.matlab.code.document+xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?\u003e\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\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eIn this game you are competing against two other people to guess the number that I'm thinking of.\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=\\\"2\\\"/\u003e\u003c/w:numPr\u003e\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eI randomly choose an\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\u003einteger\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e between\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\u003eone\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e and\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\u003eten\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e (inclusive). I don't provide any clues about the number.\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=\\\"2\\\"/\u003e\u003c/w:numPr\u003e\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eYour first opponent tries to guess the number. They guess randomly.\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=\\\"2\\\"/\u003e\u003c/w:numPr\u003e\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eYour second opponent tries to guess the number. They also guess randomly.\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=\\\"2\\\"/\u003e\u003c/w:numPr\u003e\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eYou try to guess the number. But you guess\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\u003estrategically\u003c/w:t\u003e\u003c/w:r\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=\\\"ListParagraph\\\"/\u003e\u003cw:numPr\u003e\u003cw:numId w:val=\\\"2\\\"/\u003e\u003c/w:numPr\u003e\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eThe winner is the person who guesses my chosen number, or the person who guesses closest to my chosen number. This represents a \\\"win\\\".\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=\\\"2\\\"/\u003e\u003c/w:numPr\u003e\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eIf two contestants are equally close, they may share the win, with such a result being declared a \\\"draw\\\". (It is a loss for the remaining contestant.) A draw is worth half as much as a win.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eEach person hears the guesses stated by any preceding competitors, so you will be aware of the two prior guesses (provided to you as the vector\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:rFonts w:cs=\\\"monospace\\\"/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eguessesOfOpponents\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e). Moreover, each guess must be unique.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eIf everyone guessed randomly, each person should have an equal chance of winning.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eIt might seem that you're at a disadvantage, having the last opportunity to guess. But actually you have the advantage of extra knowledge.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eBy guessing strategically, you should be able 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\u003eachieve a success rate of 45% or more\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e, in which\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003esuccess rate = (wins + draws/2) / games\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\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\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eRELATED PROBLEM:  \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\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:hyperlink w:docLocation=\\\"https://www.mathworks.com/matlabcentral/cody/problems/52323\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eProblem 52323. Guess the number I'm thinking of (Part 2)\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003c/w:p\u003e\u003c/w:body\u003e\u003c/w:document\u003e\",\"relationship\":null}],\"relationships\":[{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/document\",\"target\":\"/matlab/document.xml\",\"relationshipId\":\"rId1\"}]}"},{"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":44630,"title":"Guess the number I'm thinking of (Part 1)","description":"In this game you are competing against two other people to guess the number that I'm thinking of.\r\nI randomly choose an integer between one and ten (inclusive). I don't provide any clues about the number.\r\nYour first opponent tries to guess the number. They guess randomly.\r\nYour second opponent tries to guess the number. They also guess randomly.\r\nYou try to guess the number. But you guess strategically.\r\nThe winner is the person who guesses my chosen number, or the person who guesses closest to my chosen number. This represents a \"win\".\r\nIf two contestants are equally close, they may share the win, with such a result being declared a \"draw\". (It is a loss for the remaining contestant.) A draw is worth half as much as a win.\r\nEach person hears the guesses stated by any preceding competitors, so you will be aware of the two prior guesses (provided to you as the vector guessesOfOpponents). Moreover, each guess must be unique.\r\nIf everyone guessed randomly, each person should have an equal chance of winning.\r\nIt might seem that you're at a disadvantage, having the last opportunity to guess. But actually you have the advantage of extra knowledge.\r\nBy guessing strategically, you should be able to achieve a success rate of 45% or more, in which\r\nsuccess rate = (wins + draws/2) / games\r\n\r\nRELATED PROBLEM:  \r\nProblem 52323. Guess the number I'm thinking of (Part 2)","description_html":"\u003cdiv style = \"text-align: start; line-height: 20.44px; min-height: 0px; white-space: normal; color: rgb(0, 0, 0); font-family: Menlo, Monaco, Consolas, monospace; font-style: normal; font-size: 14px; font-weight: 400; text-decoration: none solid rgb(0, 0, 0); white-space: normal; \"\u003e\u003cdiv style=\"block-size: 484px; display: block; min-width: 0px; padding-block-start: 0px; padding-top: 0px; perspective-origin: 407px 242px; transform-origin: 407px 242px; vertical-align: baseline; \"\u003e\u003cdiv style=\"block-size: 21px; font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-block-end: 9px; margin-block-start: 2px; margin-bottom: 9px; margin-inline-end: 10px; margin-inline-start: 4px; margin-left: 4px; margin-right: 10px; margin-top: 2px; perspective-origin: 384px 10.5px; text-align: left; transform-origin: 384px 10.5px; white-space: pre-wrap; margin-left: 4px; margin-top: 2px; margin-bottom: 9px; margin-right: 10px; \"\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003eIn this game you are competing against two other people to guess the number that I'm thinking of.\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003col style=\"block-size: 160px; font-family: Helvetica, Arial, sans-serif; list-style-type: decimal; margin-block-end: 20px; margin-block-start: 10px; margin-bottom: 20px; margin-top: 10px; perspective-origin: 391px 80px; transform-origin: 391px 80px; margin-top: 10px; margin-bottom: 20px; \"\u003e\u003cli style=\"block-size: 20px; display: list-item; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-start: 56px; margin-left: 56px; margin-top: 0px; perspective-origin: 363px 10px; text-align: left; transform-origin: 363px 10px; white-space: pre-wrap; margin-left: 56px; \"\u003e\u003cspan style=\"block-size: auto; display: inline; margin-inline-start: 0px; margin-left: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003eI randomly choose an\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"block-size: auto; display: inline; margin-inline-start: 0px; margin-left: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003e \u003c/span\u003e\u003c/span\u003e\u003cspan style=\"block-size: auto; display: inline; margin-inline-start: 0px; margin-left: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; \"\u003e\u003cspan style=\"font-weight: 700; \"\u003einteger\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"block-size: auto; display: inline; margin-inline-start: 0px; margin-left: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003e between\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"block-size: auto; display: inline; margin-inline-start: 0px; margin-left: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003e \u003c/span\u003e\u003c/span\u003e\u003cspan style=\"block-size: auto; display: inline; margin-inline-start: 0px; margin-left: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; \"\u003e\u003cspan style=\"font-weight: 700; \"\u003eone\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"block-size: auto; display: inline; margin-inline-start: 0px; margin-left: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003e and\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"block-size: auto; display: inline; margin-inline-start: 0px; margin-left: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003e \u003c/span\u003e\u003c/span\u003e\u003cspan style=\"block-size: auto; display: inline; margin-inline-start: 0px; margin-left: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; \"\u003e\u003cspan style=\"font-weight: 700; \"\u003eten\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"block-size: auto; display: inline; margin-inline-start: 0px; margin-left: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003e (inclusive). I don't provide any clues about the number.\u003c/span\u003e\u003c/span\u003e\u003c/li\u003e\u003cli style=\"block-size: 20px; display: list-item; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-start: 56px; margin-left: 56px; margin-top: 0px; perspective-origin: 363px 10px; text-align: left; transform-origin: 363px 10px; white-space: pre-wrap; margin-left: 56px; \"\u003e\u003cspan style=\"block-size: auto; display: inline; margin-inline-start: 0px; margin-left: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003eYour first opponent tries to guess the number. They guess randomly.\u003c/span\u003e\u003c/span\u003e\u003c/li\u003e\u003cli style=\"block-size: 20px; display: list-item; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-start: 56px; margin-left: 56px; margin-top: 0px; perspective-origin: 363px 10px; text-align: left; transform-origin: 363px 10px; white-space: pre-wrap; margin-left: 56px; \"\u003e\u003cspan style=\"block-size: auto; display: inline; margin-inline-start: 0px; margin-left: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003eYour second opponent tries to guess the number. They also guess randomly.\u003c/span\u003e\u003c/span\u003e\u003c/li\u003e\u003cli style=\"block-size: 20px; display: list-item; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-start: 56px; margin-left: 56px; margin-top: 0px; perspective-origin: 363px 10px; text-align: left; transform-origin: 363px 10px; white-space: pre-wrap; margin-left: 56px; \"\u003e\u003cspan style=\"block-size: auto; display: inline; margin-inline-start: 0px; margin-left: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003eYou try to guess the number. But you guess\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"block-size: auto; display: inline; margin-inline-start: 0px; margin-left: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003e \u003c/span\u003e\u003c/span\u003e\u003cspan style=\"block-size: auto; display: inline; margin-inline-start: 0px; margin-left: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; \"\u003e\u003cspan style=\"font-style: italic; \"\u003estrategically\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"block-size: auto; display: inline; margin-inline-start: 0px; margin-left: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003e.\u003c/span\u003e\u003c/span\u003e\u003c/li\u003e\u003cli style=\"block-size: 40px; display: list-item; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-start: 56px; margin-left: 56px; margin-top: 0px; perspective-origin: 363px 20px; text-align: left; transform-origin: 363px 20px; white-space: pre-wrap; margin-left: 56px; \"\u003e\u003cspan style=\"block-size: auto; display: inline; margin-inline-start: 0px; margin-left: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003eThe winner is the person who guesses my chosen number, or the person who guesses closest to my chosen number. This represents a \"win\".\u003c/span\u003e\u003c/span\u003e\u003c/li\u003e\u003cli style=\"block-size: 40px; display: list-item; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-start: 56px; margin-left: 56px; margin-top: 0px; perspective-origin: 363px 20px; text-align: left; transform-origin: 363px 20px; white-space: pre-wrap; margin-left: 56px; \"\u003e\u003cspan style=\"block-size: auto; display: inline; margin-inline-start: 0px; margin-left: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003eIf two contestants are equally close, they may share the win, with such a result being declared a \"draw\". (It is a loss for the remaining contestant.) A draw is worth half as much as a win.\u003c/span\u003e\u003c/span\u003e\u003c/li\u003e\u003c/ol\u003e\u003cdiv style=\"block-size: 42px; font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-block-end: 9px; margin-block-start: 2px; margin-bottom: 9px; margin-inline-end: 10px; margin-inline-start: 4px; margin-left: 4px; margin-right: 10px; margin-top: 2px; perspective-origin: 384px 21px; text-align: left; transform-origin: 384px 21px; white-space: pre-wrap; margin-left: 4px; margin-top: 2px; margin-bottom: 9px; margin-right: 10px; \"\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003eEach person hears the guesses stated by any preceding competitors, so you will be aware of the two prior guesses (provided to you as the vector\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003e \u003c/span\u003e\u003c/span\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; \"\u003e\u003cspan style=\"font-family: Menlo, Monaco, Consolas, \u0026quot;Courier New\u0026quot;, monospace; \"\u003eguessesOfOpponents\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003e). Moreover, each guess must be unique.\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"block-size: 21px; font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-block-end: 9px; margin-block-start: 2px; margin-bottom: 9px; margin-inline-end: 10px; margin-inline-start: 4px; margin-left: 4px; margin-right: 10px; margin-top: 2px; perspective-origin: 384px 10.5px; text-align: left; transform-origin: 384px 10.5px; white-space: pre-wrap; margin-left: 4px; margin-top: 2px; margin-bottom: 9px; margin-right: 10px; \"\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003eIf everyone guessed randomly, each person should have an equal chance of winning.\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"block-size: 42px; font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-block-end: 9px; margin-block-start: 2px; margin-bottom: 9px; margin-inline-end: 10px; margin-inline-start: 4px; margin-left: 4px; margin-right: 10px; margin-top: 2px; perspective-origin: 384px 21px; text-align: left; transform-origin: 384px 21px; white-space: pre-wrap; margin-left: 4px; margin-top: 2px; margin-bottom: 9px; margin-right: 10px; \"\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003eIt might seem that you're at a disadvantage, having the last opportunity to guess. But actually you have the advantage of extra knowledge.\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"block-size: 21px; font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-block-end: 9px; margin-block-start: 2px; margin-bottom: 9px; margin-inline-end: 10px; margin-inline-start: 4px; margin-left: 4px; margin-right: 10px; margin-top: 2px; perspective-origin: 384px 10.5px; text-align: left; transform-origin: 384px 10.5px; white-space: pre-wrap; margin-left: 4px; margin-top: 2px; margin-bottom: 9px; margin-right: 10px; \"\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003eBy guessing strategically, you should be able to\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003e \u003c/span\u003e\u003c/span\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; \"\u003e\u003cspan style=\"font-weight: 700; \"\u003eachieve a success rate of 45% or more\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003e, in which\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"block-size: 21px; font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-block-end: 9px; margin-block-start: 2px; margin-bottom: 9px; margin-inline-end: 10px; margin-inline-start: 4px; margin-left: 4px; margin-right: 10px; margin-top: 2px; perspective-origin: 384px 10.5px; text-align: left; transform-origin: 384px 10.5px; white-space: pre-wrap; margin-left: 4px; margin-top: 2px; margin-bottom: 9px; margin-right: 10px; \"\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003esuccess rate = (wins + draws/2) / games\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"block-size: 21px; font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-block-end: 9px; margin-block-start: 2px; margin-bottom: 9px; margin-inline-end: 10px; margin-inline-start: 4px; margin-left: 4px; margin-right: 10px; margin-top: 2px; perspective-origin: 384px 10.5px; text-align: left; transform-origin: 384px 10.5px; white-space: pre-wrap; margin-left: 4px; margin-top: 2px; margin-bottom: 9px; margin-right: 10px; \"\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003e\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"block-size: 21px; font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-block-end: 9px; margin-block-start: 2px; margin-bottom: 9px; margin-inline-end: 10px; margin-inline-start: 4px; margin-left: 4px; margin-right: 10px; margin-top: 2px; perspective-origin: 384px 10.5px; text-align: left; transform-origin: 384px 10.5px; white-space: pre-wrap; margin-left: 4px; margin-top: 2px; margin-bottom: 9px; margin-right: 10px; \"\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003eRELATED PROBLEM:  \u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cul style=\"block-size: 20px; font-family: Helvetica, Arial, sans-serif; list-style-type: square; margin-block-end: 20px; margin-block-start: 10px; margin-bottom: 20px; margin-top: 10px; perspective-origin: 391px 10px; transform-origin: 391px 10px; margin-top: 10px; margin-bottom: 20px; \"\u003e\u003cli style=\"display: list-item; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-start: 56px; margin-left: 56px; margin-top: 0px; perspective-origin: 363px 10px; text-align: left; transform-origin: 363px 10px; white-space: pre-wrap; margin-left: 56px; \"\u003e\u003ca target='_blank' href = \"https://www.mathworks.com/matlabcentral/cody/problems/52323\"\u003e\u003cspan style=\"\"\u003e\u003cspan style=\"\"\u003eProblem 52323. Guess the number I'm thinking of (Part 2)\u003c/span\u003e\u003c/span\u003e\u003c/a\u003e\u003c/li\u003e\u003c/ul\u003e\u003c/div\u003e\u003c/div\u003e","function_template":"function y = myGuess(guessesOfOpponents)\r\n    y = 42;\r\nend","test_suite":"%% Anti-hacking test\r\nassessFunctionAbsence({'rng', 'RandStream'}, 'FileName','myGuess.m')\r\n\r\n%% Ensure unique guesses of integers, which are in-range\r\nfor j = 1 : 1000\r\n    numberToBeGuessed = randi(10);\r\n    gOO = randperm(10, 2);\r\n    mG = myGuess(gOO);\r\n    assert( mG \u003e= 1  \u0026  mG \u003c= 10 , 'Out of requested range.' )\r\n    u = unique( floor([gOO mG]) );\r\n    assert( length(u) == 3 , 'Your guess must not have been already chosen.' )\r\nend;\r\n\r\n%% maxIts: 20000 = Too small; 30000 = Not quite big enough; 35000 = Just big enough (usually!); 50000 = Big enough (usually!); 100000 = Big enough, plus safety margin \u0026 efficiency incentive (but waste of resources)\r\nmaxIts = 100000;    \r\ntic\r\nfor j = 1 : 10\r\n    WDL = [0 0 0];\r\n    for itn = 1 : maxIts\r\n        numberToBeGuessed = randi(10);\r\n        gOO = randperm(10, 2);\r\n        diffs = abs( [gOO myGuess(gOO)] - numberToBeGuessed );\r\n        winningContestant = find( min(diffs)==diffs );\r\n        if any( winningContestant == 3 ),\r\n            if length(winningContestant) == 1,\r\n                % Win\r\n                WDL(1) = WDL(1) + 1;  \r\n            else\r\n                % Draw\r\n                WDL(2) = WDL(2) + 1;  \r\n            end;\r\n        else\r\n            % Loss\r\n            WDL(3) = WDL(3) + 1;  \r\n        end;\r\n    end;\r\n    successRate = (WDL(1) + WDL(2)/2) / maxIts\r\n    assert( successRate \u003e= 0.45 )\r\nend;\r\ntoc","published":true,"deleted":false,"likes_count":13,"comments_count":6,"created_by":64439,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":69,"test_suite_updated_at":null,"rescore_all_solutions":false,"group_id":1,"created_at":"2018-05-04T14:00:17.000Z","updated_at":"2026-02-06T20:26:39.000Z","published_at":"2018-05-05T12:29:22.000Z","restored_at":null,"restored_by":null,"spam":false,"simulink":false,"admin_reviewed":false,"description_opc":"{\"parts\":[{\"partUri\":\"/matlab/document.xml\",\"contentType\":\"application/vnd.mathworks.matlab.code.document+xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?\u003e\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\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eIn this game you are competing against two other people to guess the number that I'm thinking of.\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=\\\"2\\\"/\u003e\u003c/w:numPr\u003e\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eI randomly choose an\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\u003einteger\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e between\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\u003eone\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e and\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\u003eten\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e (inclusive). I don't provide any clues about the number.\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=\\\"2\\\"/\u003e\u003c/w:numPr\u003e\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eYour first opponent tries to guess the number. They guess randomly.\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=\\\"2\\\"/\u003e\u003c/w:numPr\u003e\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eYour second opponent tries to guess the number. They also guess randomly.\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=\\\"2\\\"/\u003e\u003c/w:numPr\u003e\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eYou try to guess the number. But you guess\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\u003estrategically\u003c/w:t\u003e\u003c/w:r\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=\\\"ListParagraph\\\"/\u003e\u003cw:numPr\u003e\u003cw:numId w:val=\\\"2\\\"/\u003e\u003c/w:numPr\u003e\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eThe winner is the person who guesses my chosen number, or the person who guesses closest to my chosen number. This represents a \\\"win\\\".\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=\\\"2\\\"/\u003e\u003c/w:numPr\u003e\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eIf two contestants are equally close, they may share the win, with such a result being declared a \\\"draw\\\". (It is a loss for the remaining contestant.) A draw is worth half as much as a win.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eEach person hears the guesses stated by any preceding competitors, so you will be aware of the two prior guesses (provided to you as the vector\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:rFonts w:cs=\\\"monospace\\\"/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eguessesOfOpponents\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e). Moreover, each guess must be unique.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eIf everyone guessed randomly, each person should have an equal chance of winning.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eIt might seem that you're at a disadvantage, having the last opportunity to guess. But actually you have the advantage of extra knowledge.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eBy guessing strategically, you should be able 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\u003eachieve a success rate of 45% or more\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e, in which\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003esuccess rate = (wins + draws/2) / games\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\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\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eRELATED PROBLEM:  \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\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:hyperlink w:docLocation=\\\"https://www.mathworks.com/matlabcentral/cody/problems/52323\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eProblem 52323. Guess the number I'm thinking of (Part 2)\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003c/w:p\u003e\u003c/w:body\u003e\u003c/w:document\u003e\",\"relationship\":null}],\"relationships\":[{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/document\",\"target\":\"/matlab/document.xml\",\"relationshipId\":\"rId1\"}]}"},{"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:\"udf\"","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:\"udf\"","current_player":null,"sort":"map(difficulty_value,0,0,999) asc"},"parser":"MathWorks::Search::Solr::QueryParser","directives":{"term":{"directives":{"tag":[["tag:\"udf\"","","\"","udf","\""]]}}},"facets":{"#\u003cMathWorks::Search::Field:0x00007f9c69566ff0\u003e":null,"#\u003cMathWorks::Search::Field:0x00007f9c69566f50\u003e":null},"filters":{"#\u003cMathWorks::Search::Field:0x00007f9c69566690\u003e":"\"cody:problem\""},"fields":{"#\u003cMathWorks::Search::Field:0x00007f9c69567270\u003e":1,"#\u003cMathWorks::Search::Field:0x00007f9c695671d0\u003e":50,"#\u003cMathWorks::Search::Field:0x00007f9c69567130\u003e":"map(difficulty_value,0,0,999) asc","#\u003cMathWorks::Search::Field:0x00007f9c69567090\u003e":"tag:\"udf\""},"user_query":{"#\u003cMathWorks::Search::Field:0x00007f9c69567090\u003e":"tag:\"udf\""},"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":"cody-search","password":"78X075ddcV44","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:\"udf\"","current_player":null,"sort":"map(difficulty_value,0,0,999) asc"},"parser":"MathWorks::Search::Solr::QueryParser","directives":{"term":{"directives":{"tag":[["tag:\"udf\"","","\"","udf","\""]]}}},"facets":{"#\u003cMathWorks::Search::Field:0x00007f9c69566ff0\u003e":null,"#\u003cMathWorks::Search::Field:0x00007f9c69566f50\u003e":null},"filters":{"#\u003cMathWorks::Search::Field:0x00007f9c69566690\u003e":"\"cody:problem\""},"fields":{"#\u003cMathWorks::Search::Field:0x00007f9c69567270\u003e":1,"#\u003cMathWorks::Search::Field:0x00007f9c695671d0\u003e":50,"#\u003cMathWorks::Search::Field:0x00007f9c69567130\u003e":"map(difficulty_value,0,0,999) asc","#\u003cMathWorks::Search::Field:0x00007f9c69567090\u003e":"tag:\"udf\""},"user_query":{"#\u003cMathWorks::Search::Field:0x00007f9c69567090\u003e":"tag:\"udf\""},"queried_facets":{}},"options":{"fields":["id","difficulty_rating"]},"join":" "},"results":[{"id":44393,"difficulty_rating":"medium"},{"id":44630,"difficulty_rating":"medium"},{"id":44435,"difficulty_rating":"hard"}]}}