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)
5.rand // evenly distributed. 1.0.linrand // probability decreases linearly from 0 to <number>.
5.0.rand2 // evenly distributed.
10.bilinrand // probability is highest around 0,
// decreases linearly toward +-<number>.
1.0.sum3rand // quasi-gaussian, bell-shaped distribution.
rrand(24, 48) // linear distribution in the given range.
exprand(0.01, 1) // exponential distribution;
// both numbers must have the same sign.
// (Note that the distribution of numbers is not exactly an exponential distribution,
// since that would be unbounded: we might call it a logarithmic uniform distribution.)
20.do({ 5.rand.postln; }); // evenly distributed
20.do({ 1.0.linrand.postln; }); // probability decreases linearly from 0 to 1.0
20.do({ 5.0.rand2.postln; }); // even
20.do({ 10.bilinrand.postln; }); // probability is highest around 0,
// decreases linearly toward +-<number>.
20.do({ 1.0.sum3rand.postln; }); // quasi-gaussian, bell-shaped.
Array.fill(10, { 1000.linrand }).postln;
// or more compact:
{ 1.0.sum3rand }.dup(100)
// or:
({ 1.0.sum3rand } ! 100)
You can seed a random generator in order to repeat the same sequence of random numbers:
(
5.do({
thisThread.randSeed = 4;
Array.fill(10, { 1000.linrand}).postln;
});
)
// Just to check, no seeding:
(
5.do({ Array.fill(10, { 1000.linrand }).postln; });
)
See also Random Seed.
Demonstrate the various statistical distributions visually, with histograms:
Array.fill(500, { 1.0.rand }).plot("Sequence of 500x 1.0.rand");
Array.fill(500, { 1.0.linrand }).plot("Sequence of 500x 1.0.linrand");
Array.fill(500, { 1.0.sum3rand }).plot("Sequence of 500x 1.0.sum3rand");
Use a histogram to display how often each (integer) occurs in a collection of random numbers, :
(
var randomNumbers, histogram, maxValue = 500, numVals = 10000, numBins = 500;
randomNumbers = Array.fill(numVals, { maxValue.rand; });
histogram = randomNumbers.histo(numBins, 0, maxValue);
histogram.plot("histogram for rand 0 - " ++ maxValue);
)
A histogram for linrand:
(
var randomNumbers, histogram, maxValue = 500.0, numVals = 10000, numBins = 500;
randomNumbers = Array.fill(numVals, { maxValue.linrand; });
histogram = randomNumbers.histo(numBins, 0, maxValue);
histogram.plot("histogram for linrand 0 - " ++ maxValue);
)
A histogram for bilinrand:
(
var randomNumbers, histogram, minValue = -250, maxValue = 250, numVals = 10000, numBins = 500;
randomNumbers = Array.fill(numVals, { maxValue.bilinrand; });
histogram = randomNumbers.histo(numBins, minValue, maxValue);
histogram.plot("histogram for bilinrand" + minValue + "to" + maxValue);
)
A histogram for exprand:
(
var randomNumbers, histogram, minValue = 5.0, maxValue = 500, numVals = 10000, numBins = 500;
randomNumbers = Array.fill(numVals, { exprand(minValue, maxValue); });
histogram = randomNumbers.histo(numBins, minValue, maxValue);
histogram.plot("histogram for exprand: " ++ minValue ++ " to " ++ maxValue);
)
And for sum3rand (cheap quasi-gaussian):
(
var randomNumbers, histogram, minValue = -250, maxValue = 250, numVals = 10000, numBins = 500;
randomNumbers = Array.fill(numVals, { maxValue.sum3rand; });
histogram = randomNumbers.histo(numBins, minValue, maxValue);
histogram.plot("histogram for sum3rand " ++ minValue ++ " to " ++ maxValue);
)
All of the single-number methods also work for (Sequenceable)Collections, simply by applying the given random message to each element of the collection:
[ 1.0, 10, 100.0, \aSymbol ].rand.postln; // note: Symbols are left as they are. List[ 10, -3.0, \aSymbol ].sum3rand.postln;
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.
(
var randomNumbers, histogram, distribution, randomTable, randTableSize=200;
var minValue = -250, maxValue = 250, numVals = 10000, numBins = 500;
// create some random distribution with values between 0 and 1
distribution = Array.fill(randTableSize,
{ arg i; (i/ randTableSize * 35).sin.max(0) * (i / randTableSize) }
);
// render a randomTable
randomTable = distribution.asRandomTable;
// get random numbers, scale them
randomNumbers = Array.fill(numVals, { randomTable.tableRand * (maxValue - minValue) + minValue; });
histogram = randomNumbers.histo(numBins, minValue, maxValue);
histogram.plot("this is the histogram we got");
distribution.plot("this was the histogram we wanted");
)
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.
20.do({ 0.5.coin.postln });
biased random decision can be simulated bygenerating a single value and check against a threshhold:
20.do({ (1.0.linrand > 0.5).postln });
20.do({ (exprand(0.05, 1.0) > 0.5).postln });
// size, minVal, maxVal
Array.rand(7, 0.0, 1.0).postln;
// is short for:
Array.fill(7, { rrand(0.0, 1.0) }).postln;
// size, minVal, maxVal
List.linrand(7, 10.0, 15.0).postln;
// is short for:
List.fill(7, { 10 + 5.0.linrand }).postln;
Signal.exprand(10, 0.1, 1); Signal.rand2(10, 1.0);
choose : equal chance for each element.
10.do({ [ 1, 2, 3 ].choose.postln });
Weighted choice:
wchoose(weights) : An array of weights sets the chance for each element.
10.do({ [ 1, 2, 3 ].wchoose([0.1, 0.2, 0.7]).postln });
List[ 1, 2, 3, 4, 5 ].scramble.postln;
f = { |n=8, min=0, max=7| (min..max).scramble.keep(n) };
f.value(8, 0, 7)
curdle(probability)
The probability argument sets the chance that two adjacent elements will be separated.
[ 1, 2, 3, 4, 5, 6, 7, 8 ].curdle(0.2).postln; // big groups [ 1, 2, 3, 4, 5, 6, 7, 8 ].curdle(0.75).postln; // small groups
Also see UGens>Generators>Stochastic in the Browse: UGens>Generators>Stochastic page.
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:
("Demand UGens")
see random patterns with analogous names
Pwrand([ 1, 2, 3 ], [0.1, 0.3, 0.6], 20);
Pwalk( (0 .. 10), Prand([ -2,-1, 1, 2], inf)); random walk.some basic examples
(
Pbind(\note, Prand([ 0, 2, 4 ], inf),
\dur, 0.2
).play;
)
(
Pbind(
\note, Pxrand([ 0, 2, 4 ], inf),
\dur, 0.2
).play;
)
(
Pbind(
\note, Pwrand([ 0, 2, 4 ], [0.1, 0.3, 0.6], inf),
\dur, 0.2
).play;
)
(
Pbind(
\midinote, Pwhite(48, 72, inf),
\dur, 0.2
).play;
)
(
Pbind(
\midinote, Pbrown(48, 72, 5, inf),
\dur, 0.2
).play;
)
(
Pbind(
\midinote, Pgbrown(48, 72, 0.5, inf).round,
\dur, 0.2
).play;
)