Random123 is a library of splittable pseudo random number generators. It follows Jax's PRGN design.
import random123
from random123 import PRNGKey
var key = random123.key(123)
var samples = random123.normal[DType.float32](key^, 1000)
fn f(owned key: PRNGKey):
var newkey: PRNGKey
var subkey: PRNGKey
newkey, subkey = random123.split(key^, 2)
g(newkey^)
h(subkey^)
Implemented methods include bits
, uniform
and normal
. More to come!
Note that it is required to use the transfer operator ^
whenever using a PRNGKey
, and split
it into new keys if needed.
Otherwise, you should make sure every key is only used once.
Random123 is blazingly fast, thanks to the buildin SIMD vectors and parallelize
in Mojo.
Here is a comparison with Jax:
You might bump into a compiler bug with DType.float16
or DType.bfloat16
.
See the issue.