Random:Shuffle() doesn't work with a fresh Random if it was created with a seed

local t = {1, 2, 3, 4}
local r = Random.new(101)
r:Shuffle(t)
print(t) -- Gives a result of {1, 2, 3, 4} always 

To fix it, we must avoid specifying a seed, or use the generator once:

local t = {1, 2, 3, 4}
local r = Random.new(101)
r:NextNumber()
r:Shuffle(t)
print(t) -- Gives a correctly shuffled result.
2 Likes

Using a seed of 102 gives 4, 2, 3, 1. It seems like 101 is just a Random seed that simply doesn’t Shuffle the array.

3 Likes

{1, 2, 3, 4} is one of the valid shuffles of the {1, 2, 3, 4} array which happens to be selected with that seed value.

6 Likes

Oh oops! My bad… I must have hallucinated checking other seeds haha

2 Likes

i feel like its kind of misleading for :shuffle() to not actually shuffle though.

may need to be a feature request!

2 Likes

It’s simply how randomness works. You have a 1/24 (?) chance for the table to be shuffled in that particular order. It shuffled, but it returned the same order, out of pure luck (if you can call it luck. It’s pseudo random, so it’s predictable randomness)

5 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.