vendredi 29 mai 2020

array filled with unsimilar random int

excuse my english... i need an array to be filled with random integers, i need those integers to be very distincts from each another : (must at least be 20 units of separation between each items)

this is what i have tried so far :

var all = [];
var i = 0 ;

randomDiff();


function randomDiff()
{
  var num1 = randomNumber(10,290);                   //chose a first random num in the range...
  all[0]= num1;                                      //...put it in first index of array
do                                                   // until you have 12 items...
{
  var temp = randomNumber(10,290);                   //...you pick a temporary num              
  var j;
  for (j = 0; j < all.length; j++ )                  // for each item already in the array
  {
    if ((temp<all[i]-10)||(temp>all[i]+10))          // if the temporary num is different enough                                                            from others members...
            {
              all.push (temp);                       //then you can store it
             i++;                                    //increment until....
             console.log (all[i]);
            }                         
        }  
     }
     while (i<11)                                    // ...it is filled with 12 items in array    
}
////////////Radom in int range function///////////////////////////////////////
function randomNumber(min, max) {  
    return Math.floor(Math.random() * (max - min) + min); 
} 

  

it s not working!!, i have tried many other strategys but always unsuccessful, including foreverlasting loops... Hope you will help me, by advance thank you!!




Aucun commentaire:

Enregistrer un commentaire