Rng:next
methodRng:next() -> numberThe next number in the stream, in [0, 1).
In the guide: Query and chart data
handle10 methods
An independent seeded random stream. The same seed produces the same sequence on every platform, and separate handles never disturb each other -- the property shared-seed content generation needs and math.random cannot give.
datumhue.random.newdatumhue.grid.voronoiThe examples on this page start from:
local rng = datumhue.random.new(42)Rng:next() -> numberThe next number in the stream, in [0, 1).
In the guide: Query and chart data
Rng:int(lo: integer, hi: integer) -> integerThe next integer in the stream, in [lo, hi] inclusive. Raises when lo > hi.
-- Both ends are included: three rolls of a six-sided die.
print(rng:int(1, 6), rng:int(1, 6), rng:int(1, 6)) --> 3 2 6Rng:chance(p: number) -> booleanTrue with probability p (one draw). p at or below 0 is never true; at or above 1, always.
Rng:pick(list: any[]) -> anyOne element of the list (one draw). Raises when the list is empty.
print(rng:pick({ "sword", "shield", "potion" })) --> shieldRng:pick_weighted(list: any[], weights: number[]) -> anyOne element of the list, chosen with probability proportional to its weight (one draw). weights parallels the list one to one; zero-weight entries are never chosen. Raises when the list is empty, the lengths differ, a weight is negative or not finite, or every weight is zero.
-- Weights pair with the list one to one: 80, 15 and 5 in 100.
local tiers = { "common", "rare", "epic" }
print(rng:pick_weighted(tiers, { 80, 15, 5 })) --> commonRng:shuffle(list: any[]) -> any[]Shuffle the list in place (one draw per element past the first) and return the same table.
-- Shuffles in place and returns the same table.
local deck = { 1, 2, 3, 4, 5 }
print(rng:shuffle(deck) == deck) --> true
print(table.concat(deck, " ")) --> 2 4 5 1 3Rng:fork() -> RngA new independent stream seeded from this one (three draws). Forking lets one shared seed split into shared and local streams that never disturb each other.
local effects = rng:fork()
local twin = datumhue.random.new(42)
twin:fork()
-- Both forked once, and only rng's fork is drawn from: drawing
-- from a fork never moves the stream it came from.
effects:next()
effects:next()
print(rng:int(1, 100) == twin:int(1, 100)) --> trueRng:shuffled(list: any[]) -> any[]A shuffled copy of the list; the list itself is untouched. Same draw count as shuffle (one per element past the first), so swapping a call between the two never moves a shared stream. The safe choice for shared vocabulary tables, where an in-place shuffle would silently reorder every later draw.
-- A shuffled copy; the list keeps its order.
local names = { "a", "b", "c", "d" }
local mixed = rng:shuffled(names)
print(table.concat(names, " ")) --> a b c d
print(table.concat(mixed, " ")) --> c d a bRng:sample(list: any[], n: integer) -> any[]n distinct elements drawn without replacement (exactly n draws); the list itself is untouched. Raises when n is negative or exceeds the list length.
-- Three different elements, in the order drawn.
local picks = rng:sample({ "a", "b", "c", "d", "e" }, 3)
print(table.concat(picks, " ")) --> c b eRng:bag(n: integer) -> BagA shuffle-bag over positions 1 to n: draw deals each position exactly once per lap and reshuffles between laps. The bag forks its own stream at creation (three draws), so its laps never move this one. Raises when n is below 1.