an open-source digital signal processing and sound synthesis language
about · links · contact

Orand

INSTRUMENT design -- pseudo-random number generator object

The Orand object can be used to generate streams of pseudo-random numbers. The generating algorthm can be seeded by a particular seed value or can be set by the system clock, thus creating a different sequence of random numbers for every run of the algorithm.

It brings together and can be used to replace a number of older pseudo-random number functions scattered throughout cmix/RTcmix, including the rrand, srrand, and crandom functions.

The pseudo-random number generator used in the RTcmix distribution isn't the latest and greatest, but it seems to do the job well enough for us flaky musicians.


Constructors

    Orand()

    creates a new pseudo-random number generator with a default seed value of 1.


    Orand(int seed)

    seed is the seed value for the pseudo-random number generator created.



Access Methods

    void Orand::seed(int seed)

    starts a new random sequence using seed as the base. Different values of seed will result in different pseudo-random number sequences. The same value will yield the same sequence each time.


    void Orand::timeseed()

    will seed the pseudo-random number generator using the system clock to generate the seed value.


    float Orand::random()

    returns a floating-point pseudo-random number between 0.0 and 1.0. The sequence of random values depends on the setting of the seed value.


    float Orand::rand()

    returns a floating-point pseudo-random number between -1.0 and 1.0. The sequence of random values depends on the setting of the seed value.


    float Orand::range(float lo, float hi)

    returns a floating-point pseudo-random number between lo and hi. The sequence of random values depends on the setting of the seed value.

Examples

    #include <Ougens.h>
    
    Orand *theNoise;
    
    // this instrument will generate noise
    int MYINSTRUMENT::init(float p[], int n_args)
    {
    
    	...
    
    	theNoise = new Orand();
    	theNoise->timeseed();
    
    	...
    
    }
    
    int MYINSTRUMENT::run()
    {
    	float out[2];
    
    	...
    
    	for (i = 0; i < framesToRun(); i++)
    	{
    		out[0] = theNoise->rand();
    	}
    
    	...
    
    }