Posts

Showing posts with the label random

Random generator without repeated result

Random generator without repeated result I need to put the numbers from low to high in an array randomly. low high For example given: low = 10, high = 15 a result like [ 12, 13, 10, 14, 11] is good. [ 12, 13, 10, 14, 11] This is a simple algorithm: iterate from low to high and try to fill in the empty slots on an array. const low = 1000 const high = 1010 const diff = high - low const arr = new Array(diff) for (var i = low; i < high; i++) { let success = false while(!success) { const index = Math.floor(Math.random() * diff) if (arr[index] === undefined) { arr[index] = i success = true } console.log(`${index} was ${success ? 'available' : 'taken'}`) } } console.log(arr) The problem is: in the end where most of the elements are filled, it is harder to find an unoccupied slot in the array. My question is: is there an algorithm that constantly generates unique new numbers until all the numbers are consumed? ...

JS: Forward to a site with random parts in the URL

JS: Forward to a site with random parts in the URL by opening a HTML-site, I want to redirect to the following page: www.xyz.de?user=default&f1= OPTION1 &f2= OPTION2 &f3= OPTION3 &f4= OPTION4 &f5= OPTION5 &... etc. The value for the "OPTIONS" should be randomly selected from a list (array) for every OPTION (1,2,3,4,5,..). How can I do this with Javascript? kind regards is the problem to redirect or to generate the string? – Nina Scholz Jun 30 at 18:25 1 Answer 1 Hopefully this will help you, you didn't provide much details or a demo code, so this is from my point of understanding only. <p id="Link">Link Here</p> <p id="randoms">Randomly Sortde Array</p> <script> var o...