Saving Random:Next

r = Random.new(123);
print(r:NextNumber()); -- A: 0.55763420027725
print(r:NextNumber()); -- B: 0.56777438777716
r = Random.new(123);
print(r:NextNumber()); -- A: 0.55763420027725
print(r:NextNumber()); -- B: 0.56777438777716
print(r:NextNumber()); -- C: 0.93235765263651

So in this example, I have a Random.new() with a seed of 123.

Is there a way to save the progress of this Random object in order to produce the NextNumber() in order to get C without having to loop through the times I used NextNumber() everytime I load the seed?

Edit:
I need it in order for each player to experience a proper RNG roll by being able to continue where the last roll left off even when re-joining the game via loading from a save data.

What currently happens is that server has a specific Random object to handle a single droptable, which means if a player gets a 1/100 chance drop, that means other players are less likely to roll it until the probably balances out, or the player joins another server that hasn’t rolled a 1/100 recently or the player gets really lucky.

2 Likes

I think you COULD store the number of times it was used within a variable. (only issue with that… is that it’ll take too much power to get deeper seeds)

but i think your supposed to use math.randomseed(same_value_everytime) before using math.random() again.

im not sure how this would work using Random.new()

1 Like

Random in the math library was updated to use the same algorithms as the Random class and randomseed with a set value will also provide deterministic pseudorandom results much like passing a value to the new constructor, so the same principles would apply both ways.

1 Like

Out of curiosity’s sake, for the OP, where exactly do you need to save the Random to? If it’s just for the current session, you can store a reference to the Random object or use its Clone method to branch results which will keep the progress within the cycle and pass that around. If it needs to save across sessions, then no, no way exists for this.

Changing the seed would make results unique and there aren’t any methods to revisit or step into a pseudorandom cycle. This is something worth a feature request if it’s even possible to add it in. Something like this would probably still, even if it was internally supported, have a loop somewhere to reach the previous cycle value.

Updated post, but basically I could just save the seed and the number of times that has rolled, but that would mean that I have to roll the number of times rolled using the seed whenever I have to load a save data and could be costly in performance if the number of rolls is large.

So I just learnt that to my surprise, Random:NextNumber() is very efficient. Guess I’ll just roll the times that it has been rolled to get back to the saved spot for next roll.

local random = Random.new(123);
local timesRolled = 92901;
wait(2)
print("start roll");
local roll = 0;
local testTimer = tick();
for a=1, timesRolled do
    roll = random:NextNumber();
end
print(roll)
print(math.ceil((tick()-testTimer)*10000)/10000); -- Time took: ~0.0051s
1 Like

There is no way to get the current state of a Random object (yet?). It would be nice if Roblox provided a way to read the seed, like a readonly property or something called Seed.