LEARN VOID

Math and seeded random numbers

Clamp an out-of-range score and reproduce a sequence of random choices.

math-random.voidDownload source
use math

func entry(score: i32) -> i32:
    return math.clamp(score, 0, 10)
Input 14Expected result 10

Keep a score within bounds

Import math for typed numeric helpers. Here the scoreboard accepts values from zero to ten. Passing 14 returns 10. The same clamp returns its input when it already lies between the bounds.

Reproduce a draw

With use random, construct random.generator(42) for deterministic draws. A generator is a value. Copying it copies its current sequence position. Give separate simulations their own generators when their draws should be independent.

Choose a fallible operation

Use try_rand_range when bounds come from input and match its result before using the draw. Use the seeded generator for simulations and reproducible tests. It provides no cryptographic secrecy.

Try the operation

Both generators begin in the same state. The integer interval includes one and six. Replaying a draw with the same seed gives the same face.

replay-draw.voidDownload source
use random

func entry(game_number: i32) -> i32:
    var first = random.generator(game_number as u64)
    var replay = first
    var chosen = first.rand_range(1, 6)
    if replay.rand_range(1, 6) == chosen:
        return chosen
    return 0
Input 8Expected result 5

Continue learning with Collection helpers.