DebugRandom
A wrapper around Roblox's Random class that enables users to serialize and set the state of the Random object.
API differences:
- Added DebugRandom.deserialize
- Added DebugRandom.fromString
- Added DebugRandom.isDebugRandom
- Added property ClassName
- DebugRandom:__tostring yields a serialized result instead of "Random"
- Added DebugRandom:Serialize
- Added DebugRandom:SetSerializedState
- Added DebugRandom:CopyStateFrom
- Added DebugRandom:CloneRandom
Example use: Serialize a random number generator's state when an error occurs, so the error can be replicated using DebugRandom:SetSerializedState and DebugRandom.deserialize.
Types
SerializedDebugRandom
type
SerializedDebugRandom =
{
ClassName:
"DebugRandom"
,
Seed:
number
,
NumCount:
number
,
IntCount:
number
,
}
Properties
ClassName
This item is read only and cannot be modified. Read OnlyPrototype PropertyDebugRandom.ClassName:
"DebugRandom"
Functions
isDebugRandom
DebugRandom.
isDebugRandom
(
obj:
any
) →
boolean
Returns true
if obj
is a DebugRandom.
new
Returns a new DebugRandom object.
deserialize
Deserializes the data into a DebugRandom object. This data should have come from
the DebugRandom:Serialize()
method.
fromString
Deserializes a string into a DebugRandom object. The string should have come from the DebugRandom:__tostring()
metamethod.
Usage:
local rng = DebugRandom.new()
print(rng:NextNumber())
local str = tostring(rng)
local rngClone = DebugRandom.fromString(str)
assert(rngClone == rng)
__tostring
DebugRandom:
__tostring
(
) →
string
This metamethod serializes self
and transforms it into a string.
__eq
This metamethod tests for self
and other
values to be equal and is used by the operators ==
and ~=
.
Serialize
Returns a serialized version of the DebugRandom.
SetSerializedState
Set the DebugRandom's state. This state should have come from
the DebugRandom:Serialize()
method.
CopyStateFrom
Set the state of the calling DebugRandom to the state of the other
DebugRandom.
NextInteger
DebugRandom:
NextInteger
(
min:
number
,
max:
number
) →
number
Returns a pseudorandom integer uniformly distributed over [min, max].
NextNumber
DebugRandom:
NextNumber
(
) →
number
Returns a pseudorandom number uniformly distributed over [0, 1).
NextNumber
DebugRandom:
NextNumber
(
min:
number
,
max:
number
) →
number
Returns a pseudorandom number uniformly distributed over [min, max).
NextUnitVector
Returns a unit vector with a pseudorandom direction. Vectors returned from this function are uniformly distributed over the unit sphere.
Shuffle
DebugRandom:
Shuffle
(
tb:
table
) →
(
)
Uniformly shuffles the array part of tb in-place using NextInteger to pick indices. If there are any nil "holes" in the array part of the table, Shuffle throws an error, since shuffling could change the length.
The hash part of tb is ignored. No metamethods of tb are invoked.
The shuffle is defined to be a Fisher-Yates shuffle so the number of NextInteger calls is guaranteed to be consistent between engine versions for a given size of table.
Clone
Returns a new DebugRandom object with the same state as the original.
CloneRandom
Returns a clone of the DebugRandom's internal Random object.