Given a array A
and a seed S
(number), how do I make an algorithm to randomize the array A
with the seed S
? (Array A is fixed but then it should rearrange the elements of A
based on the seed)
Initial Attempt:
function shuffleArrayWithSeed(array, seed) {
var getRandom = Math.floor(Math.sin(seed++) * 100)
for (var i = 0;i<array.length;i++){
function lowerqw(i,getRandom){
if (Math.floor(getRandom * i)>=array.length){
return lowerqw(i,getRandom/2);
}else{
return Math.floor(getRandom * i)
}
}
var temporaryValue, randomIndex;
randomIndex = lowerqw(i,getRandom);
// Swap current element with a randomly selected element
temporaryValue = array[i];
array[i] = array[randomIndex];
array[randomIndex] = temporaryValue;
}
console.log(array)
return array;
}
But this doesn't randomize the array at all. Could you please find any flaws? Or come with a better algorithm, which is not that time consuming as size of array A increases? Note: This Question doesn't help me as the answers here, deal with those randomizing the array at complete random.
Aucun commentaire:
Enregistrer un commentaire