Skip to content
Justice Tech School

Random Numbers

Use a Generator with a seed for repeatable random data.

Example
rng = np.random.default_rng(42)
print(rng.integers(1, 7, size=5))
print(rng.random(3).round(3))
print(rng.choice(["a", "b", "c"], 4))

Result

[1 5 4 3 3]
[0.697 0.094 0.976]
['c' 'c' 'c' 'c']

Exercise

Seeded generator.

np.random.default_rng()

Quiz Test

Why set a seed?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.