Skip to content

Random Number Generators in Rel


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 (opens in a new tab) (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 (opens in a new tab):

Mersenne Twister

Mersenne Twister (opens in a new tab) 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")}]

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]]

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


Threefry (opens in a new tab) is a simplified Threefish (opens in a new tab) 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.

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

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.

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

Threefry falls in the class of counter-based random number generators (opens in a new tab) 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.

// read query
def output:uint64 = random_uint64
def output:uint128 = random_uint128

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.

// read query
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]

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.

Get Started!

Start your journey with RelationalAI today! Sign up to receive our newsletter, invitations to exclusive events, and customer case studies.

The information you provide will be used in accordance with the terms of our Privacy Policy. By submitting this form, you consent to allow RelationalAI to store and process the personal information submitted above to provide you the content requested.