sf2.random
Random numbers that are the same every time for the same seed. Use them for choices such as which enchantment an opponent gets, so a saved game always plays out the same way. Get sf2 with local sf2 = require("sf2").
sf2.random.stream
Section titled “sf2.random.stream”Make a random generator from a seed; the same seed always gives the same rolls.
Signature: sf2.random.stream(...)
Parameters
| Parameter | Type | Required |
|---|---|---|
... |
number|string |
Required |
Returns: A generator with methods integer(minimum, maximum), number() and pick(list), called with :.
When: Anywhere.
-- The same save, fight and opponent always get the same enchantment.local roll = sf2.random.stream(sf2.saves.seed(), fight.id, enemy.index)local kind = roll:pick { "bleeding", "frenzy", "stun" }The seed is 1 to 16 numbers or strings, and order and type matter (stream(7, "boss") differs from stream("boss", 7), and stream(1) from stream("1")). Each generator keeps its own position; make a new one to start over. Not for secrets.
sf2.random.integer
Section titled “sf2.random.integer”Pick a whole number from a range, all equally likely.
Signature: sf2.random.integer(field, minimum, maximum)
Parameters
| Parameter | Type | Required |
|---|---|---|
field |
string |
Required |
minimum |
integer |
Required |
maximum |
integer |
Required |
Returns: A whole number from minimum to maximum, inclusive.
When: After the profile has loaded.
-- Inside on_result, with route_rng declared in sf2.state.register:if result.won and result.step == 1 then return fights[sf2.random.integer("route_rng", 2, 3)]endfield names a saved sequence field of your mod. Each call advances the saved sequence.
sf2.random.number
Section titled “sf2.random.number”Pick a number from 0 up to, but not including, 1, for percentage chances.
Signature: sf2.random.number(field)
Parameters
| Parameter | Type | Required |
|---|---|---|
field |
string |
Required |
Returns: A number in [0, 1).
When: After the profile has loaded.
local rare = sf2.random.number("reward_rng") < 0.1Calls with the same field share one sequence with sf2.random.integer, so use a different field for each unrelated choice.