From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.ciao.gmane.io!not-for-mail From: Zelphir Kaltstahl Newsgroups: gmane.lisp.guile.user Subject: Re: Normal distribution random numbers Date: Sun, 31 May 2020 21:54:27 +0200 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Injection-Info: ciao.gmane.io; posting-host="ciao.gmane.io:159.69.161.202"; logging-data="87494"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Icedove/60.9.0 Cc: guile-user@gnu.org To: sph@posteo.eu Original-X-From: guile-user-bounces+guile-user=m.gmane-mx.org@gnu.org Sun May 31 21:54:42 2020 Return-path: Envelope-to: guile-user@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jfU2c-000Mf1-5L for guile-user@m.gmane-mx.org; Sun, 31 May 2020 21:54:42 +0200 Original-Received: from localhost ([::1]:38278 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jfU2b-0005d5-6V for guile-user@m.gmane-mx.org; Sun, 31 May 2020 15:54:41 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:47310) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jfU2S-0005cv-AL for guile-user@gnu.org; Sun, 31 May 2020 15:54:32 -0400 Original-Received: from mout02.posteo.de ([185.67.36.66]:42903) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jfU2Q-0008DG-SQ for guile-user@gnu.org; Sun, 31 May 2020 15:54:31 -0400 Original-Received: from submission (posteo.de [89.146.220.130]) by mout02.posteo.de (Postfix) with ESMTPS id A54B02400FD for ; Sun, 31 May 2020 21:54:28 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=posteo.de; s=2017; t=1590954868; bh=RWIEiFDEFwiT8UxAIZLcO2nIyL+sVUtU50Jq/SJssZo=; h=Subject:To:Cc:From:Date:From; b=g0nV3IJt2SZyc3DBaJRqmKm9jUpFn8nbW72bkMvvM4TcxVB7g+iaAoPrgEghhry6N luI5PIcg8aDWMed0hGV6ZnVO7dw7QV+d1fPgwzlBqi2IrqNRII/t6V09t3EKEQ3L8E ftPje5XEpw5+2pImL+VX/j3bQ+byRJtKuUbMVcuCU1WbyGMYTVoe8mz/ndvV4wgxsB hA8ZsnpoPU9+J3aSSFOTwEPvHWOYnWDiaqPgoJ+wctR98vWsEREdNNYyCE9dRKqrYM puAfn3zRUG7zaB4DhNZJqMf2lgUZaFqkQon+PEWE0xxPxLvzDM8uRAMGVl/xEjC2FY X6LB+q5dAvXfQ== Original-Received: from customer (localhost [127.0.0.1]) by submission (posteo.de) with ESMTPSA id 49Zpsw0HWVz6tm5; Sun, 31 May 2020 21:54:27 +0200 (CEST) In-Reply-To: Content-Language: en-US Received-SPF: pass client-ip=185.67.36.66; envelope-from=zelphirkaltstahl@posteo.de; helo=mout02.posteo.de X-detected-operating-system: by eggs.gnu.org: First seen = 2020/05/31 15:54:29 X-ACL-Warn: Detected OS = Linux 3.11 and newer X-Spam_score_int: -43 X-Spam_score: -4.4 X-Spam_bar: ---- X-Spam_report: (-4.4 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_MED=0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_MED=-2.3, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_PASS=-0.001, URIBL_BLOCKED=0.001 autolearn=_AUTOLEARN X-Spam_action: no action X-BeenThere: guile-user@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: General Guile related discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guile-user-bounces+guile-user=m.gmane-mx.org@gnu.org Original-Sender: "guile-user" Xref: news.gmane.io gmane.lisp.guile.user:16544 Archived-At: 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*)) > ~~~ > >