Random Number Generation in WarpScript

random number generation in WarpScript

In this tutorial, we will explain how to generate random numbers in WarpScript.

For example, this can be useful for generating random Geo Time Series with respect to a given probability distribution.

Uniform distribution

Generating a random number in WarpScript can be done with a single statement:

RAND

This function will generate a random number in [0.0, 1.0). The number generated is sampled uniformly in this interval.

Using this function, you can generate a random GTS like this:

NEWGTS 'randomGTS' RENAME 1 1000 <% NaN NaN NaN RAND ADDVALUE %> FOR

We obtain a GTS with samples from a uniform distribution in [0.0, 1.0). Sometimes, it is preferable to generate a random walk GTS.

For this, we can use RAND to generate deltas between successive values instead. And since we want deltas to be either positive or negative, we can use a custom macro based on RAND to sample a random number in [-1.0, 1.0):

<% RAND 2 * 1 - %> 'deltaRAND' STORE

Using deltaRAND to generate a random walk GTS can be done like this:

NEWGTS 'randomWalk' RENAME 0 'value' STORE 1 1000 <% NaN NaN NaN // geo coordinates (not used here) $value @deltaRAND + // the new value = last value + random delta DUP 'value' STORE // store the current value ADDVALUE // add the new value to the GTS %> FOR

Normal distribution

In the previous example, we used the function RAND that samples a random number from a uniform distribution. What if we wanted to sample from a normal distribution instead?

To do this, you can use a macro from our macro repository. Make sure that warpfleet-resolver is configured to pull macros from SenX macro repository (by default this is enabled). Then you can do:

10 @senx/rand/RANDNORMAL // generates the number of points given as argument

This will sample 10 points from a normal distribution with mean 0.0 and standard deviation 1.0.

You can use it to generate a random GTS from the normal distribution like this:

[ 1 1000 <% %> FOR ] [] [] [] [ 1000 @senx/rand/RANDNORMAL ] MAKEGTS

Other distributions

For other distributions, there is the WarpScript function RANDPDF. This function takes as argument a histogram representing the probability of each value of the random variable. It outputs a random generator function that you can call using EVAL.

Here is an example:

{ 'banana' 0.2 'artichoke' 0.3 'raw meat' 0.5 } RANDPDF 'generator' STORE

Now you can call this function:

[ $generator EVAL $generator EVAL $generator EVAL $generator EVAL $generator EVAL ]
Result: ["raw meat","raw meat","banana","artichoke","artichoke"]

Indeed, you can use RANDPDF to create generator functions of any kind of distribution provided you can create the input histogram.

Seeded generation

It can be useful to reproduce the results of a script. In order to be able to do that with a random generator, you must be able to give it a seed, which it will use to return samples in a deterministic way.

In WarpScript, you can seed an internal random generator using PRNG, like this:

123456789 PRNG

Henceforth, to obtain deterministic results, instead of using RAND, @senx/rand/RANDNORMAL and RANDPDF, you can use SRAND, @senx/rand/SRANDNORMAL and SRANDPDF which will use the internal random generator that was seeded by PRNG.

Conclusion

In this article, you have learned how to:

  • generate a random number
  • generate a random GTS
  • generate a random walk GTS
  • do the above points according to a uniform or normal distribution
  • do the above points according to a given probability distribution
  • do the above in a deterministic way

If you want to learn more about WarpScript, you can check out the article series on Thinking in WarpScript.

Share