Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <html>
- <head>
- <script src="http://code.jquery.com/jquery-1.11.3.min.js"></script>
- <script>
- function runTest(randSample)
- {
- var maxRun = 0; //the highest straight run of the same results (ex 10 heads in a row)
- var currentRun = 0; //the current highest run while looking at the results since the last change in result
- var runsCount = 0; //how many runs of x length where x = $runsAmount
- var runsAmount = 5; //the number of results in a row that counts as a suspicious run
- var zerosTotal = 0; //the current count of zeros(heads)
- var onesTotal = 0; //the current count of ones(tails)
- var lastResult = -1; //keep track of the last result to check for runs
- for (index = 0; index < randSample.length; index++)
- {
- var result = randSample[index]; //The current result we are looking at
- if(result.trim() == "")
- {
- continue; //this is just to make sure the result is there and it's not empty
- }
- //keeps track of the longest run of heads or tails
- if(lastResult == result)
- {
- currentRun = currentRun + 1;
- }
- else
- {
- if(currentRun > maxRun)
- {
- maxRun = currentRun;
- }
- if(currentRun >= runsAmount)
- {
- runsCount = runsCount + 1;
- }
- currentRun = 0;
- }
- //keeps track of how many zeros (heads) are in the set
- if(result == 0)
- {
- zerosTotal = zerosTotal + 1;
- }
- //keeps track of how many ones (tails) are in the set
- if(result == 1)
- {
- onesTotal = onesTotal + 1;
- }
- lastResult = result;
- }
- totalFlips = zerosTotal + onesTotal;
- return "<b>Heads:</b> " + zerosTotal + "/" + totalFlips + " flips<br><b>Tails:</b> " + onesTotal + "/" + totalFlips + " flips<br><b>Highest run length:</b> " + maxRun + " in a row<br><b>Total count of runs lasting " + runsAmount + " flips or more:</b> " + runsCount;
- }
- $(document).ready(function () {
- var numberOfTests = 10000; //10,000 is the max random.org can create at once
- $.ajax({
- url: "https://www.random.org/integers/?num=" + numberOfTests + "&min=0&max=1&col=1&base=10&format=html&rnd=new",
- success: function (data) {
- //This code creates the random.org RNG results
- $('#RNG').html(runTest($(data).find('pre').html().split("")));
- var randSample = [];
- //This code creates the javascript RNG results
- for (index = 0; index < numberOfTests; index++)
- {
- randSample[index] = '' + Math.floor((Math.random() * 2));
- }
- $('#PRNG').html(runTest(randSample));
- },
- error: function (data){
- $('div').html('There was an error getting to Random.org\'s website');
- }
- });
- });
- </script>
- </head>
- <body>
- <h1>Random Test</h1>
- <p>Definition of a "Run": A run is when you get the same result (heads or tails) in a row. For instance if you get 10 heads in a row it would be called a run of 10.<p>
- <h2>Random.org RNG results</h2>
- <div id="RNG"></div>
- <h2>Javascipts RNG results</h2>
- <div id="PRNG"></div>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement