gauss

gauss — Gaussian distribution random number generator.

Description

Gaussian distribution random number generator. This is an x-class noise generator.

Syntax

ares gauss krange
ires gauss irange
kres gauss krange
ares gauss kmean, ksdev
ires gauss imean, isdev
kres gauss kmean, ksdev

Performance

krange -- the range of the random numbers (-krange to +krange). Outputs both positive and negative numbers.

kmean -- normal distribution mean.

ksdev -- normal distribution standard deviation.

The first version of gauss returns random numbers following a normal distribution centered around 0.0 (mu = 0.0) with a variance (sigma) of krange / 3.83. Thus more than 99.99% of the random values generated are in the range -krange to +krange. If a mean value different of 0.0 is desired, this mean value has to be added to the generated numbers (see example below).

The second version takes mean and standard deviation as parameters. They use the more mathematically sound Box-Muller algorithm to compute the normal distribution.

For more detailed explanation of these distributions, see:

  1. C. Dodge - T.A. Jerse 1985. Computer music. Schirmer books. pp.265 - 286

  2. D. Lorrain. A panoply of stochastic cannons. In C. Roads, ed. 1989. Music machine . Cambridge, Massachusetts: MIT press, pp. 351 - 379.

Examples

Here is an example of the gauss opcode. It uses the file gauss.csd.

Example 403. Example of the gauss opcode.

See the sections Real-time Audio and Command Line Flags for more information on using command line flags.

<CsoundSynthesizer>

<CsOptions>
  -d -o dac
</CsOptions>

<CsInstruments>
instr 1
irange   = p4
imu      = p5
isamples = p6
indx     = 0
icount   = 1
ix       = 0.0
ix2      = 0.0

loop:
i1       gauss   irange
i1       =       i1 + imu
ix       =       ix + i1
ix2      =       ix2 + i1*i1
if i1 >= -(irange+imu) && i1 <= (irange+imu) then
  icount = icount+1
endif
         loop_lt indx, 1, isamples, loop
        
imean    =       ix / isamples                         ;mean value
istd     =       sqrt(ix2/isamples - imean*imean)      ;standard deviation
         prints "mean = %3.3f, std = %3.3f, ", imean, istd
         prints "samples inside the given range: %3.3f\%\n", icount*100.0/isamples
endin
</CsInstruments>
<CsScore>
i 1 0   0.1 1.0   0   100000  ; range = 1, mu = 0.0, sigma = 1/3.83 = 0.261
i 1 0.1 0.1 3.83  0   100000  ; range = 3.83, mu = 0.0, sigma = 1
i 1 0.2 0.1 5.745 2.7 100000  ; range = 5.745, mu = 2.7, sigma = 5.745/3.83 = 1.5
</CsScore>
</CsoundSynthesizer>


Its output should include lines like this:

mean = 0.000, std = 0.260, samples inside the given range: 99.993%
mean = 0.005, std = 0.999, samples inside the given range: 99.998%
mean = 2.700, std = 1.497, samples inside the given range: 100.000%

See Also

seed, betarand, bexprnd, cauchy, exprand, linrand, pcauchy, poisson, trirand, unirand, weibull

Credits

Author (first version): Paris Smaragdis
MIT, Cambridge
1995
Author (second version): Victor Lazzarini
Maynooth University, Ireland
2020

Precisions about mu and sigma added by François Pinot after a discussion with Joachim Heintz on the Csound List, December 2010.

Example written by François Pinot, adapted from a csd file by Joachim Heintz, December 2010.

Existed in 3.30