Random Javascript ordering with seed

I want to randomly shuffle a list of 4 elements, but with a seed so that as long as you have the same seed, you will get the same order of elements.

["a", "b", "c", "d"] 

I suppose I can get the seed using Math.random, I don't need something very accurate. How to sort by seed?

+5
source share
4 answers

jsFiddle Demo

You will need to align a random value for each value in the array, as far as I can tell. In this regard, you probably want to do something like this:

 for( var i = 0; i < length; i++ ){ seed.push(Math.random()); } 

Where do you guarantee that length is the same length as for the seed. This will be 4 for your simple example. Once this is done, you can pass the seed to your function (or sort) randomly to ensure that you get the same results. Shuffle will need to use it also in the loop

  var randomIndex = parseInt(seed[i] * (len - i)); 

So, this is what it all breaks down into

The seed function that will store the array of seeds

 var seeder = function(){ var seed = []; return { set:function(length){ for( var i = 0; i < length; i++ ){ seed.push(Math.random()); } return seed; }, get: function(){ return seed; }, clear: function(){ seed = []; } }; } 

Pretty simple shuffle

 function randomShuffle(ar,seed){ var numbers = []; for( var a = 0, max = ar.length; a < max; a++){ numbers.push(a); } var shuffled = []; for( var i = 0, len = ar.length; i < len; i++ ){ var r = parseInt(seed[i] * (len - i)); shuffled.push(ar[numbers[r]]); numbers.splice(r,1); } return shuffled; } 

Is used

 var arr = ["a", "b", "c", "d"]; var seed = seeder(); seed.set(arr.length); console.log(randomShuffle(arr,seed.get())); console.log(randomShuffle(arr,seed.get())); console.log(randomShuffle(arr,seed.get())); 
+1
source

Seededshuffle

Works in node and browser.

+1
source

You can create random numbers for sorting using the XOR Shift method. Example. Then just replace Math.random() with the old code on new Xor128(seed).make(3)[2] / 4294967296 * 2

0
source

You can achieve this by making small changes to the Mike Bostock implementation of the Fisher-Yates algorithm *:

 function shuffle(array, seed) { // <-- ADDED ARGUMENT var m = array.length, t, i; // While there remain elements to shuffle… while (m) { // Pick a remaining element… i = Math.floor(random(seed) * m--); // <-- MODIFIED LINE // And swap it with the current element. t = array[m]; array[m] = array[i]; array[i] = t; ++seed // <-- ADDED LINE } return array; } function random(seed) { var x = Math.sin(seed++) * 10000; return x - Math.floor(x); } 

* random function taken from this SO answer . This is hacked and not completely random, and most importantly not cryptographically secure ! Here's a histogram of samples (also in the comments on this answer, it takes time to start). In the end, you should use this only when these things are not significant. Alternatively, replace the random function with a better random number generator.

0
source

Source: https://habr.com/ru/post/1483230/


All Articles