## generating random variable x between [0,1]

daggaz
Posts: 157
Joined: 2012-04-18
Location: Denmark

This is probably really dumb, but I havent been able to find a straight forward way to do this yet and have been using:

`random_x = enoise(.49999) + 0.5 `

which gives me .00001 to .99999, which is close enough for most intents.. Is there a proper way to do this?

ChrLie
Posts: 266
Joined: 2009-01-06
Location: Switzerland

i guess this should do:

`random_x =abs(enoise(1)) `

daggaz
Posts: 157
Joined: 2012-04-18
Location: Denmark

Yes, that is better in that it includes the boundaries, but getting exactly zero is half the probability of any other outcome, still... (ok half of 0.00000001 is also pretty damn small)

I guess what I am asking is if there is hardcoded function random(a,b) that returns a random value x between [a,b] using a flat PDF?

[ last edited August 28, 2013 - 07:06 ]
Posts: 1966
Joined: 2007-06-29
Location: United States

daggaz wrote:
I guess what I am asking is if there is hardcoded function random(a,b) that returns a random value x between [a,b] using a flat PDF?

And the answer would be no.

John Weeks
WaveMetrics, Inc.
support@wavemetrics.com

Posts: 817
Joined: 2007-06-29
Location: United States

Try this:

```// uniform pseudo-random values on the interval [a,b)
Function uNoise(a,b)
Variable a,b

Variable d=(b-a)/2
return a+d*(enoise(1)+1)
End```

Note that the actual probability of getting a random value that is exactly 'a' is vanishingly small.

A.G.
WaveMetrics, Inc.

[ last edited August 28, 2013 - 09:31 ]
ikonen
Posts: 47
Joined: 2008-04-23
Location: United States

A slightly longer reply than John's is that if you really need to distinguish an inclusive vs. exclusive border in double precision floating point arithmetic (which is what returned by enoise)

a: you're probably doing something wrong
b: you will run into other precision and granularity problems not just at the borders

Note that
print enoise(1) has about the same odds of showing you -1 as 1 on the history, despite the different endpoint treatment, and each of -1 or 1 has about half the probability of appearing as any other multiple of 1e-6. That's not built into enoise...it's a function of print defaulting to 6 decimal digits of precision and rounding to the nearest 6 digit value. The real value returned by your expression actually has a granularity of about 1e-17, so the statistical bias of 1 vs. -1 will be about 1e-11.

Your original solution is on the right track, but you came up with the symmetric, exclusive version: 0 and 1 not included when rounding to 1e-6. If you're really trying to get a distribution which after rounding to the nearest 1e-6, could give you 0 or 1 inclusive with equal probability to any other multiple of 1e-6 then I think the answer is

random_x = enoise(0.5 + 5e-7) + 0.5

daggaz
Posts: 157
Joined: 2012-04-18
Location: Denmark

Thanks for the replies, I was really just worried I was doing it the hard way (or wrong) and am aware that the "unevenness" is vanishingly small, but it is interesting to see the finer details of how this works on a machine level.