Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lzyXW-0004E1-Ux for pgsql-hackers@arkaria.postgresql.org; Sun, 04 Jul 2021 09:35:50 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1lzyWy-0001uQ-Iv for pgsql-hackers@arkaria.postgresql.org; Sun, 04 Jul 2021 09:35:16 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lzyWy-0001ts-90 for pgsql-hackers@lists.postgresql.org; Sun, 04 Jul 2021 09:35:16 +0000 Received: from sfr-1.cri.mines-paristech.fr ([77.158.180.225] helo=smtp.cri.ensmp.fr) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lzyWu-0001Xh-VL for pgsql-hackers@lists.postgresql.org; Sun, 04 Jul 2021 09:35:15 +0000 Received: from pseudo (185.141.6.84.rev.sfr.net [84.6.141.185]) by smtp.cri.ensmp.fr (Postfix) with ESMTPSA id CC4CAC012C; Sun, 4 Jul 2021 11:37:09 +0200 (CEST) Date: Sun, 4 Jul 2021 11:35:09 +0200 (CEST) From: Fabien COELHO X-X-Sender: fabien@pseudo To: Dean Rasheed cc: Tom Lane , Aleksander Alekseev , PostgreSQL Developers Subject: Re: rand48 replacement In-Reply-To: Message-ID: References: <43a6e30b-542f-671a-e7bb-b7f78cf90e8c@enterprisedb.com> <699567.1625156441@sss.pgh.pa.us> <746421.1625164869@sss.pgh.pa.us> User-Agent: Alpine 2.22 (DEB 394 2020-01-19) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII; format=flowed List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Hello Dean, >> - moves the stuff to common and fully removes random/srandom (Tom) >> - includes a range generation function based on the bitmask method (Dean) >> but iterates with splitmix so that the state always advances once (Me) > > At the risk of repeating myself: do *not* invent your own scheme. > The problem with iterating using splitmix is that splitmix is a simple > shuffling function that takes a single input and returns a mutated > output depending only on that input. It also iterates over its 64 bits state in a round robin fashion so that the cycle size is 2^64 (it is a simple add). > So let's say for simplicity that you're generating numbers in the range > [0,N) with N=2^64-n and n<2^63. Each of the n values in [N,2^64) that > lie outside the range wanted are just mapped in a deterministic way back > onto (at most) n values in the range [0,N), making those n values twice > as likely to be chosen as the other N-n values. I do understand your point. If the value is outside the range, splitmix iterates over its seed and the extraction functions produces a new number which is tested again. I do not understand the "mapped back onto" part, the out of range value is just discarded and the loops starts over with a new derivation, and why it would imply that some values are more likely to come out. > So what you've just invented is an algorithm with the complexity of the > unbiased bitmask method, That is what I am trying to implement. > but basically the same bias as the biased integer multiply method. I did not understand. > I don't understand why you object to advancing the state more than > once. Doing so doesn't make the resulting sequence of numbers any less > deterministic. It does, somehow, hence my struggle to try to avoid it. call seed(0xdeadbeef); x1 = somepseudorand(); x2 = somepseudorand(); x3 = somepsuedorand(); I think we should want x3 to be the same result whatever the previous calls to the API. > In fact, I'm pretty sure you *have to* advance the state more than > once in *any* unbiased scheme. That's a common characteristic of all > the unbiased methods I've seen, and intuitively, I think it has to be > so. Yes and no. We can advance another state seeded by the root prng. > Otherwise, I'm happy with the use of the bitmask method, as long as > it's implemented correctly. I did not understand why it is not correct. -- Fabien.