From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.ciao.gmane.io!not-for-mail From: tantalum Newsgroups: gmane.lisp.guile.user Subject: Re: Normal distribution random numbers Date: Sun, 31 May 2020 15:12:16 +0000 Message-ID: Reply-To: sph@posteo.eu Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII; format=flowed Content-Transfer-Encoding: 7bit Injection-Info: ciao.gmane.io; posting-host="ciao.gmane.io:159.69.161.202"; logging-data="98328"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Posteo Webmail To: guile-user@gnu.org Original-X-From: guile-user-bounces+guile-user=m.gmane-mx.org@gnu.org Sun May 31 17:12:33 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 1jfPdZ-000PTw-AD for guile-user@m.gmane-mx.org; Sun, 31 May 2020 17:12:33 +0200 Original-Received: from localhost ([::1]:47550 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jfPdY-0001bz-Bq for guile-user@m.gmane-mx.org; Sun, 31 May 2020 11:12:32 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:47184) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jfPdP-0001bo-3t for guile-user@gnu.org; Sun, 31 May 2020 11:12:23 -0400 Original-Received: from mout01.posteo.de ([185.67.36.65]:44184) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jfPdN-0003to-Pl for guile-user@gnu.org; Sun, 31 May 2020 11:12:22 -0400 Original-Received: from submission (posteo.de [89.146.220.130]) by mout01.posteo.de (Postfix) with ESMTPS id 481BA160062 for ; Sun, 31 May 2020 17:12:17 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=posteo.eu; s=2017; t=1590937937; bh=tej5gPEE9r6olt6lHQvTg3oQfWIyRINm5eSmni1JeBY=; h=Date:From:To:Subject:From; b=kgwmjZJo4Lacl5nLi1AtN/3n7ZKAqlYMAth135rF4C4hn7VoVxB07NKRVdFHPNb/4 rt1TmmSjlksFCf71LsCfq+G23yRFXfX3Ial2Cjnwdp4pWyPKob/JuAioIAyaGqO7Aj Vl7OqH2PiloivrGK2L2RoVx78YqtxKoTK/+3BI5JDDWIMUOnZnNtKddi+3uZJISVEE jlnqmSgZfLaGbErDP6FFTSri9Yj6ufFRIe0+5TqgJ7848bJBQRIV+oPmCpOOUQsuv3 tvqSnhTq2fYA91+wc3n0TTQNv5YdQWexrMjtaRPqOe1xAKoPAak42dogUZK36iFkyE 82spuc6Xj4/jg== Original-Received: from customer (localhost [127.0.0.1]) by submission (posteo.de) with ESMTPSA id 49ZhcJ5vVBz6tmT for ; Sun, 31 May 2020 17:12:16 +0200 (CEST) Mail-Reply-To: sph@posteo.eu X-Sender: sph@posteo.eu Received-SPF: pass client-ip=185.67.36.65; envelope-from=sph@posteo.eu; helo=mout01.posteo.de X-detected-operating-system: by eggs.gnu.org: First seen = 2020/05/31 11:12:17 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, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_MED=-2.3, 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:16542 Archived-At: 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*)) ~~~