Advertisement
Guest User

Random.org VS JavaScript

a guest
May 26th, 2015
279
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <html>
  2.     <head>
  3.         <script src="http://code.jquery.com/jquery-1.11.3.min.js"></script>
  4.         <script>
  5.             function runTest(randSample)
  6.             {
  7.                 var maxRun = 0; //the highest straight run of the same results (ex 10 heads in a row)
  8.                 var currentRun = 0; //the current highest run while looking at the results since the last change in result
  9.                 var runsCount = 0; //how many runs of x length where x = $runsAmount
  10.                 var runsAmount = 5; //the number of results in a row that counts as a suspicious run
  11.                 var zerosTotal = 0; //the current count of zeros(heads)
  12.                 var onesTotal = 0; //the current count of ones(tails)
  13.                 var lastResult = -1; //keep track of the last result to check for runs
  14.  
  15.                 for (index = 0; index < randSample.length; index++)
  16.                 {
  17.                     var result = randSample[index]; //The current result we are looking at
  18.                    
  19.                     if(result.trim() == "")
  20.                     {
  21.                         continue; //this is just to make sure the result is there and it's not empty
  22.                     }
  23.                    
  24.                     //keeps track of the longest run of heads or tails
  25.                     if(lastResult == result)
  26.                     {
  27.                         currentRun = currentRun + 1;
  28.                     }
  29.                     else
  30.                     {
  31.                         if(currentRun > maxRun)
  32.                         {
  33.                             maxRun = currentRun;
  34.                         }
  35.                         if(currentRun >= runsAmount)
  36.                         {
  37.                             runsCount = runsCount + 1;
  38.                         }
  39.                         currentRun = 0;
  40.                     }
  41.                    
  42.                     //keeps track of how many zeros (heads) are in the set
  43.                     if(result == 0)
  44.                     {
  45.                         zerosTotal = zerosTotal + 1;
  46.                     }
  47.                    
  48.                     //keeps track of how many ones (tails) are in the set
  49.                     if(result == 1)
  50.                     {
  51.                         onesTotal = onesTotal + 1;
  52.                     }
  53.  
  54.                     lastResult = result;
  55.                 }
  56.  
  57.                 totalFlips = zerosTotal + onesTotal;
  58.  
  59.                 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;
  60.             }
  61.  
  62.             $(document).ready(function () {
  63.                 var numberOfTests = 10000;  //10,000 is the max random.org can create at once
  64.                 $.ajax({
  65.                     url: "https://www.random.org/integers/?num=" + numberOfTests + "&min=0&max=1&col=1&base=10&format=html&rnd=new",
  66.                     success: function (data) {
  67.                    
  68.                         //This code creates the random.org RNG results
  69.                         $('#RNG').html(runTest($(data).find('pre').html().split("")));
  70.                        
  71.                         var randSample = [];
  72.                        
  73.                         //This code creates the javascript RNG results
  74.                         for (index = 0; index < numberOfTests; index++)
  75.                         {
  76.                             randSample[index] = '' + Math.floor((Math.random() * 2));
  77.                         }
  78.                         $('#PRNG').html(runTest(randSample));
  79.                     },
  80.                     error: function (data){
  81.                         $('div').html('There was an error getting to Random.org\'s website');
  82.                     }
  83.                 });
  84.             });
  85.         </script>
  86.     </head>
  87.     <body>
  88.         <h1>Random Test</h1>
  89.         <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>
  90.         <h2>Random.org RNG results</h2>
  91.         <div id="RNG"></div>
  92.         <h2>Javascipts RNG results</h2>
  93.         <div id="PRNG"></div>
  94.     </body>
  95. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement