class Rooibos::Command::Random
A command that generates a random value.
Games roll dice. Encryption needs keys. Shuffling needs seeds. All of these call Kernel#rand or Random. But randomness in Update is a side effect: the same model and message would produce different results on each call. Testing would become fragile and non-deterministic.
This command delegates to Ruby’s Random class through the runtime. Update receives the result as a Message::Random.
Use it for dice rolls, shuffles, key generation, or any feature that needs random values without side effects in Update.
Prefer the Command.random factory method for convenience.
Example: Dice roll
def update(msg, model) case msg in { type: :random, envelope: :roll, value: } model.with(die_face: value) end end
Example: Random float
def update(msg, model) case msg in { type: :random, envelope: :spawn_chance, value: } model.with(should_spawn: value < 0.3) end end
Public Instance Methods
Source
# File lib/rooibos/command/random.rb, line 55 def call(out, token) if token.canceled? out.put(Message::Canceled.new(command: self)) return end value = if args.first.is_a?(Symbol) method_name = args.first ::Random.public_send(method_name, *args[1..]) else ::Random.rand(*args) end out.put(Ractor.make_shareable(Message::Random.new(envelope:, value:))) end