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 1m08kX-0000ss-CH for pgsql-hackers@arkaria.postgresql.org; Sun, 04 Jul 2021 20:29:57 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1m08kW-0002nM-7o for pgsql-hackers@arkaria.postgresql.org; Sun, 04 Jul 2021 20:29:56 +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 1m08kV-0002nD-W9 for pgsql-hackers@lists.postgresql.org; Sun, 04 Jul 2021 20:29:55 +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 1m08kS-0006Hl-Ny for pgsql-hackers@lists.postgresql.org; Sun, 04 Jul 2021 20:29:55 +0000 Received: from pseudo (185.141.6.84.rev.sfr.net [84.6.141.185]) by smtp.cri.ensmp.fr (Postfix) with ESMTPSA id 99A0AC0149; Sun, 4 Jul 2021 22:31:49 +0200 (CEST) Date: Sun, 4 Jul 2021 22:29:48 +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: <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: multipart/mixed; boundary="2056645051-169391880-1625430589=:2359988" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk This message is in MIME format. The first part should be readable text, while the remaining parts are likely unreadable without MIME-aware tools. --2056645051-169391880-1625430589=:2359988 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8BIT > The important property of determinism is that if I set a seed, and then > make an identical set of calls to the random API, the results will be > identical every time, so that it's possible to write tests with > predictable/repeatable results. Hmmm… I like my stronger determinism definition more than this one:-) >> I would work around that by deriving another 128 bit generator instead >> of splitmix 64 bit, but that is overkill. > > Not really relevant now, but I'm pretty sure that's impossible to do. > You might try it as an interesting academic exercise, but I believe > it's a logical impossibility. Hmmm… I was simply thinking of seeding a new pg_prng_state from the main pg_prng_state with some transformation, and then iterate over the second PRNG, pretty much like I did with splitmix, but with 128 bits so that your #states argument does not apply, i.e. something like: /* select in a range with bitmask rejection */ uint64 pg_prng_u64_range(pg_prng_state *state, uint64 range) { /* always advance state once */ uint64 next = xoroshiro128ss(state); uint64 val; if (range >= 2) { uint64 mask = mask_u64(range-1); val = next & mask; if (val >= range) { /* copy and update current prng state */ pg_prng_state iterator = *state; iterator.s0 ^= next; iterator.s1 += UINT64CONST(0x9E3779B97f4A7C15); /* iterate till val in [0, range) */ while ((val = xoroshiro128ss(&iterator) & mask) >= range) ; } } else val = 0; return val; } The initial pseudo-random sequence is left to proceed, and a new PRNG is basically forked for iterating on the mask, if needed. -- Fabien. --2056645051-169391880-1625430589=:2359988--