The Ability to Save and Load the state of a Random object

Currently given the current API of the Random class you can not save the state that your Random object is currently at.

local Randomizer = Random.new(1234)

Randomizer:NextNumber()
Randomizer:NextNumber()
Randomizer:NextNumber() --// OK I'm done but I want to save this as next times starting point

What if I wanted to start my Random sequence back where it started in a different place? I would have to call :NextNumber to set everything up.

local Randomizer = Random.new(1234)
local nthTerm = 3

--// Call `NextNumber` 3 times to "load" to the specified starting point. 
for i = 1, nthTerm do
    Randomizer:NextNumber()
end

Randomizer:NextNumber()

It would be much nicer if you had the ability to load a “state” or “nthSequence” type parameter to Random.new that sets the “current iteration” to the passed parameter or a method to do so.

local Randomizer = Random.new(1234, 3) --// Seed 1234, Position 3

Randomizer:NextInteger()

or

local Randomizer = Random.new(1234)

Randomizer:SetSequencePoint(3)
Randomizer:NextInteger()

This #platform-feedback:engine-features is explicitly based on this question:

6 Likes