• ### Gaussian numbers in a range

Hello,

below I built a patch to generate random numbers with a Gaussian distribution (mean=500; s.d.=200), but only accepting values ​​within a range (300,700).
What I have done is the correct way to do this? Or is there an easier way (to optimize performance in realtime)?

Thanks,

• Posts 9 | Views 2932
• @atux

Without further information we can not say if this distribution meets your requirements.
This is rejecting numbers above the limits. The bell-shape is cut.

Surely [gauss] is the most expensive object in your screenshot. I can not say which method is used here to generate normal-distributed pseudo-random numbers. To which lib does [gauss] belong?
(There are several ways to do that, see Wikipdia or other sources: https://en.wikipedia.org/wiki/Normal_distribution#Computational_methods )

One slight performance improvement would be to replace the second bang by [trigger bang] or in short [t b], - because visible GUI elements are slowing down Pd Vanilla.

CPU-wise the cheapest is to read a pre-generated set of random numbers, at the cost of some memory-space.

I would say, if your hardware is not slow or your project enormous, be happy and move on?

• @lacuna

Thank you, for gauss object I used maxlib, so you can try it, while I don't know what method is used to generate Gaussian numbers, I'd be interested to know.
Yes, the bell-shape is cut, I want to exclude any values ​​that are too high or too low.

a.

• @ingox

Thanks,

I see that with the nramdon object you can set the upper limit (the lower one is zero) and the mean is exactly in the middle, the range is centered around the mean. But I'm also interested in an asymmetrical interval, in my example I can do it.

Bye,

a.

• @atux Yes, if you are interested you can have a look at [array random] which produces random numbers according to the distribution within the array. This is what [nrandom] is doing. So you could generate any desired distribution and generate random numbers from this.

But to your original question: It is possible that you get many numbers outside the boundaries in a row which would lead to a stack overflow error. If this occurs, you could use [del] to break the loop. Other than this the method is valid and i have no idea to improve it.

• Yes, a stack-overflow could happen.
And a cut bell is not a bell.

@atux said:

what method is used to generate Gaussian numbers, I'd be interested to know.

the source:
https://github.com/electrickery/pd-maxlib/blob/master/src/gauss.c
seems like it uses that central-limit theorem method, resulting in a Irwin–Hall distribution, which already has limited boundaries.
https://en.wikipedia.org/wiki/Central_limit_theorem

Offtopic::: A problem might be, that it has to sum 12 independent random values, but the pseudo random function rand() of course has a dependency.

Once I have been using the Box-Muller method and saw a recurring spiral in it's output, because of the dependend set of pseudo-random numbers I used.

How random is random is a can of worms but it might be good enough for sound : )

@Ingox idea of randomizing a given distribution is neat.

• @lacuna Since it is possible to build a Turing machine out of Conways Game of Life, with enough time and rocks a computer could be build from gliders that calculates each state of the universe on a quantum level. A set of butterflies could be programmed so that a flap of their wings would change the outcome of everything. Just a slight change in the configuration would generate a different result of a coin toss or a rolled dice. But would it be true randomness? And which side of the coin would be up if nobody is even looking?

• @lacuna said:

Box-Muller method

Hello,

I rebuilt the initial patch, using the Box-Muller transform to generate random numbers with a Gaussian distribution.
It should work.

gauss_range_02.pd

Thanks for the others methods you have recommended to me, now I study them calmly.

P.S.: I see that it is now possible to use devices to generate true (non-pseudo) random numbers using quantum techniques:

http://www.randomnumbers.info/

Posts 9 | Views 2932
Internal error.

Oops! Looks like something went wrong!