unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
* Generating "independent" random numbers
@ 2023-10-03 15:08 Zelphir Kaltstahl
  2023-10-03 16:04 ` tomas
  2023-10-03 18:25 ` Maxime Devos
  0 siblings, 2 replies; 12+ messages in thread
From: Zelphir Kaltstahl @ 2023-10-03 15:08 UTC (permalink / raw)
  To: guile-user

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


^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2023-10-18 19:08 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-10-03 15:08 Generating "independent" random numbers Zelphir Kaltstahl
2023-10-03 16:04 ` tomas
2023-10-03 16:16   ` tomas
2023-10-03 22:17     ` Zelphir Kaltstahl
2023-10-03 22:16   ` Zelphir Kaltstahl
2023-10-03 18:25 ` Maxime Devos
2023-10-03 22:22   ` Zelphir Kaltstahl
2023-10-04 16:14     ` Keith Wright
2023-10-10 22:03       ` Maxime Devos
2023-10-10 23:04         ` Keith Wright
2023-10-11 11:31           ` Maxime Devos
2023-10-18 19:08     ` Mikael Djurfeldt

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).