guile-user
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Generating "independent" random numbers


From: Zelphir Kaltstahl
Subject: Re: Generating "independent" random numbers
Date: Tue, 3 Oct 2023 22:22:09 +0000

On 10/3/23 20:25, Maxime Devos wrote:
Op 03-10-2023 om 17:08 schreef Zelphir Kaltstahl:
Some time ago, I wanted to generate uniformly distributed floats though. There seems to be no facility in Guile to do that. So I took a look at Wikipedia: https://en.wikipedia.org/wiki/Normal_distribution#Computational_methods:

 > An easy-to-program approximate approach that relies on the central limit theorem is as follows: generate 12 uniform U(0,1) deviates, add them all up, and subtract 6 – the resulting random variable will have approximately standard normal distribution. In truth, the distribution will be Irwin–Hall, which is a 12-section eleventh-order polynomial approximation to the normal distribution. This random deviate will have a limited range of (−6, 6).[55] Note that in a true normal distribution, only 0.00034% of all samples will fall outside ±6σ.

OK, for most purposes this seems good enough?
That's _normal_, not uniform, like tomas wrote.

Aye, sorry for that typo. Yes, my goal is normal distributed floats (leaving aside the finite nature of the computer and floats).

Though, if you really want to (inefficient), you could apply the cdf of the normal distribution to the normal random variable to get a

First, I'll say that there is no such thing as an uniformly distributed float (*), because the real line has length infinity.

As such, you need to pick a bounded range from which you want to sample uniformly.  For example, let's say [0,1), which can be rescaled as desired to any finite half-closed interval.

The uniform distribution on [0,1) has infinite support which makes things difficult for computers, but it can be approximated by a distribution with finite support, let's say

   mu_N = sum(i=0..(N-1)) dirac(i/N)/N

where dirac(i/N) is a Dirac-pulse situated as i/N and N is large.

Up to scaling, this is simply the uniform discrete measure on {0,1,..,N-1}!

So, to generate an (approximately) uniform random number on [0,1), you can simply do

(define (random-real)
  (exact->inexact (/ (random N) N)))

for a suitably large choice of integer N>0.

(*) Ignoring for a moment there are technically finitely many floats, but the uniform distribution on the discrete set of floats is likely not what you are interested in.

(I probably didn't have to go in so much detail but whatever ...)

That's what I get for writing the wrong word :D

OK that's some math to unpack, thank you!

Best regards,
Zelphir

--
repositories: https://notabug.org/ZelphirKaltstahl




reply via email to

[Prev in Thread] Current Thread [Next in Thread]