Number statistics report in Mojo
A seeded sample run through min, max, mean, and spread.
from std.random import random_float64, seed
def smallest(values: List[Float64]) -> Float64:
var best = values[0]
for value in values:
if value < best:
best = value
return best
def largest(values: List[Float64]) -> Float64:
var best = values[0]
for value in values:
if value > best:
best = value
return best
def mean(values: List[Float64]) -> Float64:
var total = 0.0
for value in values:
total += value
return total / Float64(len(values))
def main():
# A fixed seed keeps the report reproducible.
seed(17)
var readings = List[Float64]()
for _ in range(12):
readings.append(random_float64(0.0, 100.0))
var low = smallest(readings)
var high = largest(readings)
print("readings:", len(readings))
print("min: ", low)
print("max: ", high)
print("mean: ", mean(readings))
print("spread: ", high - low)
How it works
seed(17)fixes the generator so the report repeats exactly.- Twelve
random_float64draws fill aList[Float64]. smallest,largest, andmeaneach fold the list into one number.mainprints the summary as a small aligned report.
Keywords and builtins used here
Float64Listdefforiflargestlenmainmeanprintrangereturnsmallestvar
The run, in numbers
- Lines
- 36
- Characters to type
- 812
- Tokens
- 205
- Three-star pace
- 75 tpm
At the three-star pace of 75 tokens a minute, this run takes about 164 seconds.
Step 1 of 1 in Encore, step 26 of 26 in Language basics.