From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from lists.gentoo.org (pigeon.gentoo.org [208.92.234.80]) by finch.gentoo.org (Postfix) with ESMTP id 0BF1B1387FD for ; Fri, 6 Jun 2014 19:38:17 +0000 (UTC) Received: from pigeon.gentoo.org (localhost [127.0.0.1]) by pigeon.gentoo.org (Postfix) with SMTP id A4AD4E0B4E; Fri, 6 Jun 2014 19:38:11 +0000 (UTC) Received: from mout.gmx.net (mout.gmx.net [212.227.17.22]) (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by pigeon.gentoo.org (Postfix) with ESMTPS id 701AAE0B33 for ; Fri, 6 Jun 2014 19:38:10 +0000 (UTC) Received: from gmx.de ([88.65.187.210]) by mail.gmx.com (mrgmx001) with ESMTPSA (Nemesis) id 0LcSAg-1WTKNj02hq-00jpAT for ; Fri, 06 Jun 2014 21:38:09 +0200 Date: Fri, 6 Jun 2014 21:37:52 +0200 From: null_ptr To: gentoo-user@lists.gentoo.org Subject: Re: [gentoo-user] OT: Mapping random numbers (PRNG) Message-ID: <20140606193752.GA17757@gmx.de> Mail-Followup-To: gentoo-user@lists.gentoo.org References: <20140606025619.GB3837@solfire> <20140606183928.GC4718@solfire> Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-Id: Gentoo Linux mail X-BeenThere: gentoo-user@lists.gentoo.org Reply-to: gentoo-user@lists.gentoo.org MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii; format=flowed Content-Disposition: inline In-Reply-To: <20140606183928.GC4718@solfire> User-Agent: Mutt/1.5.22 (2013-10-16) X-Provags-ID: V03:K0:TFwFFAFdkWHME+P7uH6kKYZGxD9zsa/PVCTRHZrUKS/oUiYxYlk u4TzxtD992bXQDwbkf7Z8k1HB21FMts7fqD7jQ4AxxICc3jLuE+U29QqttjMe6XCdyz74Jc zQzZA/kkEYkggkeZ4cUdggwYoUU15aOwih7rIHeqvhdXXXADxItT0dtZv0TcLqB4mw0K0zV vIvCcyFWwHtAfYiBhE2VA== X-Archives-Salt: aa309976-6a3e-4601-ae23-904e62bddfd6 X-Archives-Hash: fba55d09d7610ce1e96d68a43d54dc19 On 06/06/14 20:39, meino.cramer@gmx.de wrote: >To get an better imagination of that...suppose the rand() would only >return numbers in the range of 1...12 and the alphabet has only 8 >characters (as 2^32 is not devideable by 62) > >rand(): >1 2 3 4 5 6 7 8 9 10 11 12 > >rand()%N : rand()%7 >1 2 3 4 5 6 7 0 1 2 3 4 > >or in other words: An even distribution of numbers of rand() >would result in a unevenly distributed sequence of characters...or? >This would break the quality of ISAACs output. > >I am sure I did something wrong here...but where is the logic trap? You thought on that is totally right. Let's say random numbers have the rande [0..RAND_MAX] Blindly calculating modulus creates a bias towards lower numbers if RAND_MAX+1 is not divisible by N. For most applications this bias is negligible. If you really want the same distribution you have to discard numbers greater than RAND_MAX - ((RAND_MAX+1)%N) (Beware the undefined behaviour in the above line if you try C.) Basically you want one less than the largest by N dividable number in your range. With numbers: rand(): 0 1 2 3 4 5 6 7 8 9 10 11 12 rand()%5 with discarding >12-((12+1)%5)=9: 0 1 2 3 4 0 1 2 3 4 discarded --- null_ptr Please don't follow me.