ruby - How to shuffle an array the same way every time? -
calling array#shuffle shuffles array randomly, want shuffle reproducibly – i.e., same way every time. there method that? want call, example, shuffle_with_key(123) , same result every time.
array#shuffle can take seeded random instance.
a = [1,2,3,4] seed = 1 a.shuffle(random: random.new(seed)) # => [4, 1, 3, 2] a.shuffle(random: random.new(seed)) # => [4, 1, 3, 2] just replace seed = 1 whatever random seed want use.
Comments
Post a Comment