Learn how to generate random numbers in WarpScript. Very useful to generate random Time Series for synthetic test datasets.

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:

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:

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

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

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:

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:

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:

Now you can call this function:

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:

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

GTS

walk GTS

And also to do the above:

points according to a uniform or normal distribution

points according to a given probability distribution

in a deterministic way

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

Machine Learning Engineer