lundi 19 octobre 2020

Shuffle an array of size 4 given rand in [0,1]

I've tried to take the first 4 digits after the decimal point % 4 + 1 and rewrite the array to fit the digits. For example, if the array is [1, 2, 3, 4] and rand gave 0.8765 then the new array would be [1, 4, 3, 2]. The problem is what happens if i get 0.1111 and such? I could rerun rand() until i get a good permutation but i think there's a simpler solution?




Aucun commentaire:

Enregistrer un commentaire