javascript - generate two arrays of randoms between an interval -
i'd use function generate 2 arrays of same length values of first , second array shouldnt duplicates , neither values in first array shall not in second one.
what i've tried far:
function generatesiruri(width){ var exists = false; for(var = 0; < width; i++){ var randomnumber = math.floor(math.random()*101); var secondrandomnumber = math.floor(math.random()*101); if(sir1[i] == randomnumber && sir2[i] == secondrandomnumber && randomnumber == secondrandomnumber && sir1[i] == secondrandomnumber){ exists = true; return; } if(!exists){ sir1.push(randomnumber); sir2.push(secondrandomnumber); } } sortascending(sir1); sortascending(sir2); } function sortascending(array){ var sort; for(i = 0; < array.length; i++){ for(var j = 0; j < array.length; j++){ if(array[j] > array[j + 1]){ sort = array[j]; array[j] = array[j + 1]; array[j + 1] = sort; } } } return; }
also, if have better option sort these arrays sortascending function , better option of if condition in generatesiruri please suggest. can't see better option now. thx!
do this:
while(sir1.length < width) { var randomnumber = math.floor(math.random()*101); if (sir1.indexof(randomnumber) == -1) sir1.push(randomnumber); } while(sir2.length < width) { var randomnumber = math.floor(math.random()*101); if ((sir2.indexof(randomnumber) == -1) && (sir1.indexof(randomnumber) == -1)) sir2.push(randomnumber); }
this way, you're first filling sir1 unique values , separately filling sir2 unique values not in sir1.
Comments
Post a Comment