unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
From: Zelphir Kaltstahl <zelphirkaltstahl@posteo.de>
To: guile-user@gnu.org
Subject: Generating "independent" random numbers
Date: Tue,  3 Oct 2023 15:08:03 +0000	[thread overview]
Message-ID: <3e30da69-f5bd-44ed-b4f1-bd084c06e415@posteo.de> (raw)

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


             reply	other threads:[~2023-10-03 15:08 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-10-03 15:08 Zelphir Kaltstahl [this message]
2023-10-03 16:04 ` Generating "independent" random numbers 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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/guile/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=3e30da69-f5bd-44ed-b4f1-bd084c06e415@posteo.de \
    --to=zelphirkaltstahl@posteo.de \
    --cc=guile-user@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).