I have an array which is being get by a HTTP request every times the user reloads the page, and I want to shuffle it daily.
I want this array to be shuffled day by day and keep the same array the whole day, for all the users. The shuffle order has to be set the first time a user is getting this array and have to be the same for all users the whole day, beginning at midnight, every time the array is beign get by the request.
Then the next day, shuffle again for all users, the whole day but differently. And so on.
Here's my shuffle function:
function shuffle (array) {
let currentIndex = array.length; let randomIndex
// While there remain elements to shuffle...
while (currentIndex !== 0) {
// Pick a remaining element...
randomIndex = Math.floor(Math.random() / 10) * currentIndex)
currentIndex--;
// And swap it with the current element.
[array[currentIndex], array[randomIndex]] = [
array[randomIndex], array[currentIndex]]
}
return array
}
I'm not so good at algorithmic (only basics) and some help on this would be welcome.
Thanks !
CodePudding user response:
Your code looks like it might be JavaScript. If so, look at Seeding the random number generator in Javascript for seedable random number generators. Use that to make the random decisions with your current sorting algorithm.
I would suggest starting with a seed that is a hash of some fixed garbage string and the current date. This will change once a day, but the information about what you got yesterday won't leak the secret of the fixed garbage string.