Guides | Random

# Randomness

Randomness in SC

As in any computer program, there are no "truly random" number generators in SC. They are pseudo-random, meaning they use very complex, but deterministic algorithms to generate sequences of numbers that are long enough and complicated enough to seem "random" for human beings. (i.e. the patterns are too complex for us to detect.)

If you start a random number generator algorithm with the same "seed" number several times, you get the same sequence of random numbers. (See example below, randomSeed)

## Create single random numbers

### Seeding

You can seed a random generator in order to repeat the same sequence of random numbers:

### Histograms

Demonstrate the various statistical distributions visually, with histograms:

Use a histogram to display how often each (integer) occurs in a collection of random numbers, :

A histogram for linrand:

A histogram for bilinrand:

A histogram for exprand:

And for sum3rand (cheap quasi-gaussian):

### on Collections

All of the single-number methods also work for (Sequenceable)Collections, simply by applying the given random message to each element of the collection:

### Arbitrary random distributions

An integral table can be used to create an arbitrary random distribution quite efficiently. The table building is expensive though. The more points there are in the random table, the more accurate the distribution.

## Random decisions

`coin` simulates a coin toss and results in true or false. 1.0 is always true, 0.0 is always false, 0.5 is 50:50 chance.

biased random decision can be simulated bygenerating a single value and check against a threshhold:

## Random choice from Collections

`choose` : equal chance for each element.

Weighted choice:

`wchoose(weights)` : An array of weights sets the chance for each element.

## Randomly group a Collection

The probability argument sets the chance that two adjacent elements will be separated.

## Random signal generators, i.e. UGens

Also see UGens>Generators>Stochastic in the Browse: UGens>Generators>Stochastic page.

### Random operators on signals

Unary or binary random method produce a random value for each frame (not implemented in some cases). This can be used to implement tendency masks.

### UGens that generate random numbers once, or on trigger:

Rand
uniform distribution of float between (lo, hi), as for numbers.
IRand
uniform distribution of integer numbers.
TRand
uniform distribution of float numbers, triggered
TIRand
uniform distribution of integer numbers, triggered
LinRand
skewed distribution of float numbers, triggered
NRand
sum of n uniform distributions, approximates gaussian distr. with higher n.
ExpRand
exponential distribution
TExpRand
exponential distribution, triggered
CoinGate
statistical gate for a trigger
TWindex
triggered weighted choice between a list.

### Seeding

Like using randSeed to set the random generatorsfor each thread in sclang, you can choose which of several random generators on the server to use, and you can reset (seed) these random generators:

### UGens that generate random numbers on demand

("Demand UGens")

see random patterns with analogous names

## Random Patterns

Prand
choose randomly one from a list ( list, numRepeats)
Pxrand
choose one element from a list, no repeat of previous choice
Pwhite
within range [<hi>, <lo>], choose a random value.
Pbrown
within range [<hi>, <lo>], do a random walk with a maximum <step> to the next value.
Pgbrown
geometric brownian motion
Plprand
Phprand
Pmeanrand
Pbeta
Pcauchy
Pgauss
Ppoisson
Pexprand
Pwrand
choose from a list, probabilities by weights
Pshuf
scramble the list, then repeat that order <repeats> times.
Pwalk
`Pwalk( (0 .. 10), Prand([ -2,-1, 1, 2], inf));` random walk.
Pfsm
random finite state machine pattern, see its help file. see also MarkovSet in MathLib quark
Pseed
sets the random seed for that stream.

some basic examples