# Random Number Generators in Rel

Stefan Pabst

27 January 2022

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.533183 2 "February" 0.454029

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:

 1 0.77284 2 0.126125 3 0.0466869 4 0.432299 5 0.829248

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:

 :float64 0.144638 :uint64 1.99575e+18

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:

 1 0.295901 2 0.597137 3 0.425932

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:

 :uinit128 269725298896846294947893034277537324294 :uinit64 10203492138328006656

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:

 MT Threefry 1 0.19743756585023298 0.9549170970961249 2 0.025825079715451782 0.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.