You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+29-1Lines changed: 29 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -52,7 +52,7 @@ _RETURNS_
52
52
*__value__ - Random element from the array.
53
53
54
54
### splitmix64.weightedchoice(t)
55
-
Takes table `t` where keys are choices and values are weights. Returns a random key. Weights must be ≥ 0. If all weights are 0 or a weight is negative, an error is raised.
55
+
Takes table `t` where keys are choices and values are weights. Returns a random key. Keys are sorted deterministically (numbers first, then strings lexicographically) before selection, so the result is reproducible for the same seed across all platforms. Weights must be ≥ 0. If all weights are 0 or a weight is negative, an error is raised.
56
56
57
57
_PARAMETERS_
58
58
*__t__ <kbd>table</kbd> - Table of key-weight pairs.
@@ -110,6 +110,34 @@ local state = splitmix64.state()
110
110
splitmix64.randomseed(state) -- restore exact state
111
111
```
112
112
113
+
### splitmix64.new_instance([seed])
114
+
Creates a new independent RNG instance. Each instance has its own internal state, completely isolated from the global module state and from other instances. This allows running multiple RNG streams in parallel without interference.
115
+
116
+
_PARAMETERS_
117
+
*__seed__ <kbd>number</kbd> or <kbd>string</kbd> _(optional)_ — Initial seed for the instance. Defaults to 0.
118
+
119
+
_RETURNS_
120
+
*__rng__ <kbd>table</kbd> — A table with the same set of functions as the module: `random`, `randomseed`, `state`, `randomchoice`, `weightedchoice`, `toss`, `dice`.
121
+
122
+
#### Example
123
+
124
+
```lua
125
+
-- Create two independent generators
126
+
localrng_enemies=splitmix64.new_instance(42)
127
+
localrng_loot=splitmix64.new_instance(999)
128
+
129
+
-- Each has its own state — they don't affect each other or the global module
130
+
print(rng_enemies.random(1, 100))
131
+
print(rng_loot.random(1, 100))
132
+
133
+
-- The global state is unchanged
134
+
print(splitmix64.random())
135
+
136
+
-- Save/restore instance state
137
+
localsaved=rng_enemies.state()
138
+
rng_enemies.randomseed(saved)
139
+
```
140
+
113
141
### Advanced Usage
114
142
115
143
You can also globally substitute the built-in `math.random` with `splitmix64`:
Copy file name to clipboardExpand all lines: splitmix64/api/splitmix64.script_api
+13-1Lines changed: 13 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -42,7 +42,7 @@
42
42
desc: Random element from the array.
43
43
- name: weightedchoice
44
44
type: function
45
-
desc: Takes table t where keys are choices and values are weights. Returns a random key. Weights must be >= 0. If all weights are 0 or a weight is negative, an error is raised.
45
+
desc: Takes table t where keys are choices and values are weights. Returns a random key. Keys are sorted deterministically (numbers first, then strings lexicographically) before selection, so the result is reproducible for the same seed. Weights must be >= 0. If all weights are 0 or a weight is negative, an error is raised.
46
46
parameters:
47
47
- name: t
48
48
type: table
@@ -58,6 +58,18 @@
58
58
- name: value
59
59
type: number
60
60
desc: Returns 0 or 1.
61
+
- name: new_instance
62
+
type: function
63
+
desc: Creates a new RNG instance with independent state. Optional seed (number or string) for reproducibility.
64
+
parameters:
65
+
- name: seed
66
+
optional: true
67
+
type: number|string
68
+
desc: Optional seed for the new instance.
69
+
returns:
70
+
- name: rng
71
+
type: userdata
72
+
desc: RNG instance with methods random, randomseed, state, randomchoice, weightedchoice, toss, dice. Same API as the module but with isolated state.
61
73
- name: dice
62
74
type: function
63
75
desc: Roll one or more dice of the given type. Returns a table of individual rolls and the total sum.
0 commit comments