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. 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 ...)

Best regards,
Maxime Devos.

Attachment: OpenPGP_0x49E3EE22191725EE.asc
Description: OpenPGP public key

Attachment: OpenPGP_signature
Description: OpenPGP digital signature

Reply via email to