Join us at Snowflake Summit June 26-29 in Las Vegas!

Random Number Generators in Rel

Stefan Pabst

27 January 2022

less than a minute read

We are excited to share that we have once again expanded the functionality of our Relational Knowledge Graph Management System (RKGMS). We have implemented multiple pseudorandom number generators (PRNGs) that can now be used to build probabilistic models in Rel. Specifically, three general-purpose PRNGs are currently available via our Rel Standard Library:

Mersenne Twister

Mersenne Twister is one of the most widely used PRNGs. In Rel, the relation random_mersenne_twister generates a floating-point random number (between 0.0 and 1.0) for every tuple in a relation:

random_mersenne_twister[42, {(1, "January"); (2, "February")}]

Relation: output

1"January"0.5331830160438613
2"February"0.4540291355871424

Providing the seed (here: 42) ensures that the Rel model is deterministic and reproducible—meaning it always re-evaluates to the same pseudorandom number.

As well as numbers, DateTime data can also be used as seeds.

random_mersenne_twister[datetime_now, range[1, 5, 1]]

Relation:

10.7728398958252762
20.12612519968264668
30.046686894291055214
40.4322992163128061
50.8292480967676699

The relation range can be used to conveniently create five random numbers.

Threefry

Threefry is a simplified Threefish algorithm which uses a UInt64 state space.

The relations random_threefry_uint64 and random_threefry_float64 can be used to create random UInt64 and Float64 numbers.

def output:uint64 = random_threefry_uint64[1234, 3]
def output:float64 = random_threefry_float64[1234, 3]

Relation:

:float640.14463837673485513
:uint641995746028264696295

The random floating-point numbers lie between 0.0 and 1.0, whereas the random UInt64 numbers are distributed over the entire UInt64 value range.

In contrast to the Mersenne Twister implementation, the Threefry implementation is first-order, meaning it expects individual Int64 or UInt64 values rather than relations with arity greater than 1.

It is even possible to create independent random sequences, where the seed itself is randomly generated.

def seed = random_threefry_uint64[1234, 0]
def output = random_threefry_float64[seed, i]
    for i in range[1, 3, 1]

Relation:

10.2959011588531637
20.5971368640131505
30.4259318598026802

Threefry falls in the class of counter-based random number generators which are well-suited for generating random numbers in a highly parallelized fashion using multiple CPU threads or GPUs.

Random Device

The relations random_unit64 and random_uint128 use a hardware-based random device provided by the system to generate random numbers.

def output:uint64 = random_uint64
def output:uint128 = random_uint128

Relation:

:uinit128269725298896846294947893034277537324294
:uinit6410203492138328006656

No seed is needed because the randomness is hardware-based.

Due to the nature of the random device, the generated random numbers are not reproducible and change for every transaction. This can have huge performance impact and negative consequences for incremental view maintenance as values change every time this relation is re-evaluated.

In contrast to the Mersenne Twister and Threefry PRNGs, the relations random_unit64 and random_uint128 produce only single random numbers and cannot produce sequences of random numbers in Rel.

The random number from random_unit64 can also be used as a seed for Mersenne Twister and Threefry, chaining different PRNGs together.

def data = {1; 2}

def my_numbers:MT =
    random_mersenne_twister[random_uint64, data]

def my_numbers:threefry =
    random_threefry_float64[random_uint64, i]
    for i in data

def output = table[my_numbers]

Relation:

MTThreefry
10.197437565850232980.9549170970961249
20.0258250797154517820.9055341717489653

What’s Next?

If you have a favorite PRNG that you would like to see in Rel, please let us know.

Stay tuned as we are developing a uniform interface which can be used across all our pseudorandom number generators.

Related Posts

Get Early Access

Join our community, keep up to date with the latest developments in our monthly newsletter, and get early access to RelationalAI.