typestar

Random numbers in Mojo

Seeding first makes a random run reproducible, which is what benchmarks need.

from std.random import seed, random_float64, random_si64


def main():
    seed(7)
    for _ in range(3):
        print(random_float64(0.0, 1.0))
    print("die roll", random_si64(1, 6))

How it works

  1. seed(7) pins the generator so the sequence repeats.
  2. random_float64 draws a float between the two bounds.
  3. random_si64 draws a signed integer between them instead.

Keywords and builtins used here

The run, in numbers

Lines
8
Characters to type
166
Tokens
44
Three-star pace
80 tpm

At the three-star pace of 80 tokens a minute, this run takes about 33 seconds.

Type this snippet

Step 2 of 5 in Numbers & time, step 13 of 22 in Parameters & SIMD.

← Previous Next →

Random numbers in other languages