unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
From: Zelphir Kaltstahl <zelphirkaltstahl@posteo.de>
To: sph@posteo.eu
Cc: guile-user@gnu.org
Subject: Re: Normal distribution random numbers
Date: Sun, 31 May 2020 21:54:27 +0200	[thread overview]
Message-ID: <e27b2d81-217d-7a7e-f912-8e4b3c96b264@posteo.de> (raw)
In-Reply-To: <e349a94c5b81b63e88759a258e4186d4@posteo.de>

Hi!

Interesting technique, that when thinking about it, intuitively makes
sense to me. Thanks for sharing!

Regards,
Zelphir   

On 31.05.20 17:12, tantalum wrote:
> surely not the ideal way to generate numbers with a normal
> distribution, but there is a way to use custom probabilities from a
> list, which i think is nice to know.
> it works like this:
>
> have a list of probabilities. can be as long as you want. i think that
> is called probability density.
> ->
> (1 0 3 1)
>
> create the cumulative sums for this list. that is, sums like this
>   (a b c ...) -> (a (+ a b) (+ a b c) ...)
> i think that is called cumulative distribution.
> ->
> (1 1 4 5)
>
> create a random number up to the largest sum
> ->
> (random 5)
>
> return the first index of the list of cumulative sums that is greater
> than the random number.
> given the distribution above, you would see index 2 a lot, never index
> 1, and index 0 and 3 rarely.
>
> ~~~
> (use-modules ((srfi srfi-1) #:select (last)))
>
> (define (cusum a . b)
>   "calculate cumulative sums from the given numbers.
>    (a b c ...) -> (a (+ a b) (+ a b c) ...)"
>   (cons a (if (null? b) (list) (apply cusum (+ a (car b)) (cdr b)))))
>
> (define* (random-discrete-f probabilities #:optional (state
> *random-state*))
>   "(real ...) [random-state] -> procedure:{-> integer}"
>   (let* ((cuprob (apply cusum probabilities)) (sum (last cuprob)))
>     (lambda ()
>       (let ((deviate (random sum state)))
>         (let loop ((a 0) (b cuprob))
>           (if (null? b) a (if (< deviate (car b)) a (loop (+ 1 a) (cdr
> b)))))))))
>
> (define random* (random-discrete-f (list 1 0 3 1)
> (random-state-from-platform)))
> (display (random*))
> ~~~
>
>



  reply	other threads:[~2020-05-31 19:54 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-05-31 15:12 Normal distribution random numbers tantalum
2020-05-31 19:54 ` Zelphir Kaltstahl [this message]
  -- strict thread matches above, loose matches on Subject: below --
2020-05-30 20:21 Zelphir Kaltstahl
2020-05-30 20:42 ` Zelphir Kaltstahl
2020-06-04 15:03   ` Mikael Djurfeldt
2020-06-04 15:08     ` Zelphir Kaltstahl
2020-06-04 15:11       ` Mikael Djurfeldt
2020-06-04 15:22         ` Mikael Djurfeldt
2020-05-30 21:30 ` Arne Babenhauserheide
2020-05-30 23:16   ` Zelphir Kaltstahl

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=e27b2d81-217d-7a7e-f912-8e4b3c96b264@posteo.de \
    --to=zelphirkaltstahl@posteo.de \
    --cc=guile-user@gnu.org \
    --cc=sph@posteo.eu \
    /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).