RANDOM
Random Processes
The relative frequency of occurrence of a random variable can be described by a probability function (for discrete random variables) or by density functions (for continuous random variables).
When two dice are thrown simultaneously, the sum x of their numbers can be 2, 3, ...12. The following figure shows the probability function p(x) of these possible outcomes. p(x) is always less than or equal to 1. The sum of the probabilities of all possible outcomes is 1.
For continuous random variables the probability to get a specific value x is 0. But the probability to get a value out of a certain interval can be indicated by an area that corresponds to this probability. The function f(x) over these areas is called density function. With the following density the chance to get a number smaller than 0 is 0, to get a number between 0 and 0.5 is 0.5, to get a number between .5 and 1 is 0.5 etc. Density functions f(x) can reach values greater than 1 but the area under the function is 1.
Generating Random Numbers With a Given Probability or Density
Csound provides opcodes for some specific densities but no means to produce random number with user defined probability or density functions. The opcodes rand_density and rand_probability (see below) generate random numbers with probabilities or densities given by tables. They are realized with the so-called rejection sampling method.
Rejection Sampling:
The principle of rejection sampling is first to generate uniformly distributed random numbers in the range required and then to accept these values corresponding to a given density function (otherwise to reject them). Let us demonstrate the method using the density function shown in the next figure. (Since the rejection sampling method only uses the "shape" of the function the area under the function need not be 1). We first generate uniformly distributed random numbers rnd1 over the interval [0, 1]. Of these we accept a proportion corresponding to f(rnd1). For example, the value 0.32 will only be accepted in the proportion of f(0.32) = 0.82. We do this by generating a new random number rand2 between 0 and 1 and accept rnd1 only if rand2 < f(rnd1) and reject it otherwise. (see Signals, Systems and Sound Synthesis chapter 10.1.4.4)
rejection sampling
EXAMPLE Appendix_Random01_Rejection_Sampling.csd
<CsoundSynthesizer> <CsOptions> -odac </CsOptions> <CsInstruments> ;example by martin neukom sr = 44100 ksmps = 10 nchnls = 1 0dbfs = 1 ; random number generator to a given density function ; kout random number; k_minimum,k_maximum,i_fn for a density function opcode rand_density, k, kki kmin,kmax,ifn xin loop: krnd1 random 0,1 krnd2 random 0,1 k2 table krnd1,ifn,1 if krnd2 > k2 kgoto loop xout kmin+krnd1*(kmax-kmin) endop ; random number generator to a given probability function ; kout random number ; in: i_nr number of possible values ; i_fn1 function for random values ; i_fn2 probability function opcode rand_probability, k, iii inr,ifn1,ifn2 xin loop: krnd1 random 0,inr krnd2 random 0,1 k2 table int(krnd1),ifn2,0 if krnd2 > k2 kgoto loop kout table krnd1,ifn1,0 xout kout endop instr 1 krnd rand_density 400,800,2 aout poscil .1,krnd,1 out aout endin instr 2 krnd rand_probability p4,p5,p6 aout poscil .1,krnd,1 out aout endin </CsInstruments> <CsScore> ;sine f1 0 32768 10 1 ;density function f2 0 1024 6 1 112 0 800 0 112 1 ;random values and their relative probability (two dice) f3 0 16 -2 2 3 4 5 6 7 8 9 10 11 12 f4 0 16 2 1 2 3 4 5 6 5 4 3 2 1 ;random values and their relative probability f5 0 8 -2 400 500 600 800 f6 0 8 2 .3 .8 .3 .1 i1 0 10 ;i2 0 10 4 5 6 </CsScore> </CsoundSynthesizer>
Random Walk
In a series of random numbers the single numbers are independent of each other. Parameter (left figure) or paths in the room (two-dimensional trajectory in the right figure) created by random numbers wildly jump around.
Example 1
Table[RandomReal[{-1, 1}], {100}];
We get a smoother path, a so-called random walk, by adding at every time step a random number r to the actual position x (x += r).
Example 2
x = 0; walk = Table[x += RandomReal[{-.2, .2}], {300}];
The path becomes even smoother by adding a random number r to the actual velocity v.
v += r
x += v
The path can by bounded to an area (figure to the right) by inverting the velocity if the path exceeds the limits (min, max):
vif(x < min || x > max) v *= -1
The movement can be damped by decreasing the velocity at every time step by a small factor d
v *= (1-d)
Example 3
x = 0; v = 0; walk = Table[x += v += RandomReal[{-.01, .01}], {300}];
The path becomes again smoother by adding a random number r to the actual acelleration a, the change of the acelleration, etc.
a += r
v += a
x += v
Example 4
x = 0; v = 0; a = 0;
Table[x += v += a += RandomReal[{-.0001, .0001}], {300}];
(see Signals, Systems and Sound Synthesis chapter 10.2.3.2)
EXAMPLE Appendix_random02_Random_Walk.csd
<CsoundSynthesizer> <CsInstruments> ;example by martin neukom sr = 44100 ksmps = 128 nchnls = 1 0dbfs = 1 ; random frequency instr 1 kx random -p6, p6 kfreq = p5*2^kx aout oscil p4, kfreq, 1 out aout endin ; random change of frequency instr 2 kx init .5 kfreq = p5*2^kx kv random -p6, p6 kv = kv*(1 - p7) kx = kx + kv aout oscil p4, kfreq, 1 out aout endin ; random change of change of frequency instr 3 kx init .5 kfreq = p5*2^kx ka random -p7, p7 kv = kv + ka kv = kv*(1 - p8) kx = kx + kv kv = (kx < -p6 || kx > p6?-kv : kv) aout oscili p4, kfreq, 1 out aout endin <CsInstruments> <CsScore> ; i1 p4 p5 p6 ; i2 p4 p5 p6 p7 ; amp c_fr rand damp ; i2 0 20 .1 600 0.01 0.001 ; amp c_fr d_fr rand damp ; amp c_fr rand ; i1 0 20 .1 600 0.5 ; i3 p4 p5 p6 p7 p8 i3 0 20 .1 600 1 0.001 0.001 <CsScore> <CsoundSynthesizer>