Random Seed:
Reference | Core > Kernel | Random

Random Seed

Random generator seed


Every Thread in sclang has a (pseudo-) random number generator that is responsible for all randomization within this thread. Each random number generator has its own seed (starting point) from which the series of values is generated. This seed can be set and after that, the randgen (being strictly deterministic) produces exactly the same numbers again.

In order to save disk space, you can reproduce any sequence of randomized data just by one Integer number that you can write down in your notebook.


Inheriting Seeds

When a routine is created, it inherits its random number generator from the parent thread (thisThread). If no particular randSeed is set (the normal case), then the same random number generator object provides numbers to both the parent and the child (one stream of random numbers, distributed between both).

The parent thread is whichever Routine is in force at the moment of creating a new Routine. If no Routine is evaluating at that moment, then the parent is the sclang process main Thread. By default, all Routines inherit the random number generator from the main thread.

To set the main random seed for the entire application, then, evaluate thisThread.randSeed = yourSeed outside of the context of any Routine, as in the example below.

When you set randSeed on a Routine, the Routine switches to its own random number generator, independent of any other. Subsequently, Routines created within this Routine will inherit its unique random number generator.

Note that this inheritance takes place only at the moment of creating a new Routine. Calling next or play on the child Routine does not override that Routine's random number generator.

Audio example