Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Your algorithm is very ineffective. When array is almost filled up, you literally do millions of useless iterations until you're in luck and RNG accidentally picks missing number. Rewrite it to:</p> <ol> <li>Generate an array of all possible numbers - from 1 to 99.</li> <li>When you need a random numbers, generate a random index in current bounds of this array, splice element and this random position, removing it from array and use its value as your desired random number.</li> <li>If generated numbers don't fit some of your conditions (minDiff?) return them back to array. Do note, that you can still stall in loop forever if everything that is left in array is unable to fit your conditions.</li> </ol> <p>Every value you pull from array in this way is guaranteed to be unique, since you originally filled it with unique numbers and remove them on use.</p> <p>I've stripped drawing and placed generated numbers into array that you can check in console. Put your drawing back and it should work - numbers are generated instantly now:</p> <pre><code>var snakes = ['./Images/Snakes/snake1.png','./Images/Snakes/snake2.jpg','./Images/Snakes/snake3.gif','./Images/Snakes/snake4.gif','./Images/Snakes/snake5.gif','./Images/Snakes/snake6.jpg']; var ladders = ['./Images/Ladders/ladder1.jpg','./Images/Ladders/ladder2.jpg','./Images/Ladders/ladder3.png','./Images/Ladders/ladder4.jpg','./Images/Ladders/ladder5.png']; function drawTable() { // Now generating snakes. generateRand(snakes,0); generateRand(ladders,1); } var uniqNumbers = [] for(var idx = 1; idx &lt; 100; idx++){ uniqNumbers.push(idx) } var results = [] function generateRand(arr,flag) { var valFlag = 0; var minDiff = 8; // Minimum difference between start of snake/ladder to its end. var temp; for(var i = 0; i&lt; arr.length; ++i) { var valid = false // This is the single place it still can hang, through with current size of arrays it is highly unlikely do { var random_1 = uniqNumbers.splice(Math.random() * uniqNumbers.length, 1)[0] var random_2 = uniqNumbers.splice(Math.random() * uniqNumbers.length, 1)[0] if (Math.abs(random_1 - random_2) &lt; minDiff) { // return numbers uniqNumbers.push(random_1) uniqNumbers.push(random_2) } else { valid = true } } while (!valid); if(flag == 0) // Snake { if(random_1 &lt; random_2) // Swapping them if the first number is smaller than the second number. { var temp = random_1; random_1 = random_2; random_2 = temp } } else // Ladders { if(random_1&gt;random_2) // Swapping them if the first number is greater than the second number. { var temp = random_1; random_1 = random_2; random_2 = temp } } // Just for debug - look results up on console results.push([random_1, random_2]) } } drawTable() </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload