[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Generating "independent" random numbers
From: |
Zelphir Kaltstahl |
Subject: |
Generating "independent" random numbers |
Date: |
Tue, 3 Oct 2023 15:08:03 +0000 |
Hello Guile Users,
today I want to verify some understanding I have about generating random numbers
using Guile.
So there is SRFI-27 and I am using it like this:
~~~~
(define make-random-integer-generator
(lambda* (#:key (seed #f))
"Get a procedure for generating uniformly distributed
random integers from 0 up to a not included bound, which is
seeded by the keyword argument seed, which must be a
positive integer."
(cond
[seed
(let ([rand-src (make-random-source)])
;; Set the given seed to guarantee same results for
;; same invokations.
(random-source-pseudo-randomize! rand-src 0 seed)
;; Obtain a procedure, which gives uniformly
;; distributed integers.
(random-source-make-integers rand-src))]
[else
(let ([rand-src (make-random-source)])
;; Try to make the random source truly random. How
;; this works depends on the specific implementation
;; of SRFI-27.
(random-source-randomize! rand-src)
(random-source-make-integers rand-src))])))
~~~~
That gives me a simple procedure, that I can call with no argument to get the
next random number from the RNG. It allows me to see the RNG, which I definitely
want for being able to reproduce results.
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?
But there is one caveat:
https://en.wikipedia.org/wiki/Irwin%E2%80%93Hall_distribution
> In probability and statistics, the Irwin–Hall distribution, named after
Joseph Oscar Irwin and Philip Hall, is a probability distribution for a random
variable defined as the sum of a number of independent random variables, each
having a uniform distribution.[1] For this reason it is also known as the
uniform sum distribution.
Aha! They need to be "independent" too! Not only uniformly distributed! Might be
my code would be working wrong, if I do not pay attention to statistical traps.
Checking SRFI-27:
https://www.gnu.org/software/guile/manual/html_node/SRFI_002d27-Random-Sources.html#index-random_002dsource_002dpseudo_002drandomize_0021
> Function: random-source-pseudo-randomize! source i j
> Changes the state of the random source s into the initial state of the (i,
j)-th independent random source, where i and j are non-negative integers. This
procedure provides a mechanism to obtain a large number of independent random
sources (usually all derived from the same backbone generator), indexed by two
integers. In contrast to random-source-randomize!, this procedure is entirely
deterministic.
The wording here "the (i, j)-th independent random source" makes me think, that
if i and j are set with 0 and the seed like in my procedure, the generated
random integers are _not_ independent.
Is this understanding correct?
Am I correct in assuming, that I will need to make 12 separate RNGs with
different values for the tuple (i, j), to generate 12 independent uniformly
distributed random integers?
Best regards,
Zelphir
--
repositories:https://notabug.org/ZelphirKaltstahl
- Generating "independent" random numbers,
Zelphir Kaltstahl <=