 # Randomize a series of numbers without ever repeating

I need a solution to randomize all element without ever repeating

Example

I have this values stored in variables
1
2
3

I need to have random this possibile combination
1 (first random)
3 (second random)
2 (third random)

Without ever repeating

What time of solution i can use ?

If a have a long list of variable there are more combination.

I can randomize an array but i can not know a solution to repeat function if value is already used.

This is a simple macro to create random numbers i want to show all different result

``````{
"Name": "Random_Number_Min_Max",
"CreationDate": "2020-2-4",
"Commands": [
{
"Command": "executeScript_Sandbox",
"Target": "var min = 1000; var max = 1500; var randomNumber = Math.floor(Math.random() * (max - min + 1)) + min; return randomNumber;",
"Value": "Random_Number"
},
{
"Command": "echo",
"Target": "\${Random_Number}",
}
]
}
``````

Echo must show always different numbers.

Thanks

Put the list in an arrany and then use `shuffle(array)` => It shuffles (randomly reorders) elements of the array.

@ulrich

Can you post an example of shuffle command please ?

Thanks

A well-known unbiased shuffle algorithm is the Fisher-Yates (aka Knuth) Shuffle.

See https://medium.com/@nitinpatel_20236/how-to-shuffle-correctly-shuffle-an-array-in-javascript-15ea3f84bfb for an interesting story about shuffling arrays correctly You can see a great visualization of the Fisher-Yates shuffle here.

We can use the same code inside executescript_sandbox to shuffle (randomize) any array:

Shuffle array macro, it randomizes a list of animals:

``````{
"Name": "shuffle",
"CreationDate": "2020-7-8",
"Commands": [
{
"Command": "store",
"Target": "fast",
"Value": "!replayspeed"
},
{
"Command": "echo",
"Target": "Fill array with animals in alphabetic order",
"Value": ""
},
{
"Command": "executeScript_Sandbox",
"Target": "return new Array ('ape','cat','deer','dog','frog','horse','lion','seal','whale','zebra')",
"Value": "names"
},
{
"Command": "executeScript_Sandbox",
"Target": "return \${names}.length",
"Value": "len"
},
{
"Command": "echo",
"Target": "array length = \${len}",
"Value": ""
},
{
"Command": "forEach",
"Target": "names",
"Value": "elem"
},
{
"Command": "echo",
"Target": "\${elem}",
"Value": "blue"
},
{
"Command": "end",
"Target": "",
"Value": ""
},
{
"Command": "comment",
"Target": "Shuffle array with  Fisher-Yates (aka Knuth) Shuffle",
"Value": ""
},
{
"Command": "executeScript_Sandbox",
"Target": "var array = \${names}; var currentIndex = array.length, temporaryValue, randomIndex;   while (0 !== currentIndex) {     randomIndex = Math.floor(Math.random() * currentIndex);     currentIndex -= 1;    temporaryValue = array[currentIndex];     array[currentIndex] = array[randomIndex];     array[randomIndex] = temporaryValue;   }    return array; ",
"Value": "names2"
},
{
"Command": "forEach",
"Target": "names2",
"Value": "elem"
},
{
"Command": "echo",
"Target": "\${elem}",
"Value": "green"
},
{
"Command": "end",
"Target": "",
"Value": ""
}
]
}``````
1 Like

Working well, thanks admin solved 