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
seed(7)pins the generator so the sequence repeats.random_float64draws a float between the two bounds.random_si64draws a signed integer between them instead.
Keywords and builtins used here
defformainprintrange
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.
Step 2 of 5 in Numbers & time, step 13 of 22 in Parameters & SIMD.