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

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

1 | 0.7728398958252762 |

2 | 0.12612519968264668 |

3 | 0.046686894291055214 |

4 | 0.4322992163128061 |

5 | 0.8292480967676699 |

The relation `range`

can be used to conveniently create five random numbers.

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.14463837673485513 |

:uint64 | 1995746028264696295 |

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.2959011588531637 |

2 | 0.5971368640131505 |

3 | 0.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.

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 |

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.

We are excited to announce worksheets, a new interface for submitting Rel queries. Worksheets allow you to develop blocks of Rel code and run them against a database. They can be shared with other users using their URLs.

Read MoreWe are excited to announce the support of varargs in Rel. You can use varargs to write more general code that works for multiple arities. Varargs can be useful when writing generic relations for common utilities.

Read More