pg.ddx.io  pgsql-hackers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Fabien COELHO <coelho@cri.ensmp.fr>
To: Dean Rasheed <dean.a.rasheed@gmail.com>
Cc: Tom Lane <tgl@sss.pgh.pa.us>
Cc: Aleksander Alekseev <aleksander@timescale.com>
Cc: PostgreSQL Developers <pgsql-hackers@lists.postgresql.org>
Subject: Re: rand48 replacement
Date: Sun, 4 Jul 2021 22:29:48 +0200 (CEST)
Message-ID: <alpine.DEB.2.22.394.2107042204240.2359988@pseudo> (raw)
In-Reply-To: <CAEZATCWnrVACs2FM219dSkh1ivic8KiGkPEVPtu-MO3nCOYm7A@mail.gmail.com>
References: <alpine.DEB.2.22.394.2105241211230.165418@pseudo>
	<alpine.DEB.2.22.394.2105241310350.165418@pseudo>
	<CAJ7c6TM2qJdtehyLA79gFtZ+h7jwRrksK4ZAW7boxgQJqQkRAw@mail.gmail.com>
	<alpine.DEB.2.22.394.2105241509200.165418@pseudo>
	<699567.1625156441@sss.pgh.pa.us>
	<alpine.DEB.2.22.394.2107011842240.2143494@pseudo>
	<746421.1625164869@sss.pgh.pa.us>
	<CAEZATCXvzDSNhH2uRMUt5K_vmMBDrvo+D7Sn66--1Z_ELz==5Q@mail.gmail.com>
	<alpine.DEB.2.22.394.2107012212500.2248546@pseudo>
	<CAEZATCVpnOjrwU=4712OPjNDqV2sVkkK7XHDiR-nzYDL8N6z=Q@mail.gmail.com>
	<alpine.DEB.2.22.394.2107022340250.2359988@pseudo>
	<alpine.DEB.2.22.394.2107030902400.2359988@pseudo>
	<CAEZATCW76GZ-uA-i5_EL4ZukZuvWE_tTNPgRsPkR=0fyiiztTA@mail.gmail.com>
	<alpine.DEB.2.22.394.2107041115130.2359988@pseudo>
	<CAEZATCXcmccPQismOioD5kr2NMyMoeLpDaesizzoKh8M-BK5Dw@mail.gmail.com>
	<alpine.DEB.2.22.394.2107041718570.2359988@pseudo>
	<CAEZATCWnrVACs2FM219dSkh1ivic8KiGkPEVPtu-MO3nCOYm7A@mail.gmail.com>


> 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.

view thread (59+ messages)  latest in thread

Message-ID: <alpine.DEB.2.22.394.2107042204240.2359988@pseudo>
Permalink:  ../alpine.DEB.2.22.394.2107042204240.2359988@pseudo/
Also on:    postgresql.org/message-id/alpine.DEB.2.22.394.2107042204240.2359988@pseudo

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-hackers@postgresql.org
  Cc: coelho@cri.ensmp.fr, dean.a.rasheed@gmail.com, tgl@sss.pgh.pa.us, aleksander@timescale.com
  Subject: Re: rand48 replacement
  In-Reply-To: <alpine.DEB.2.22.394.2107042204240.2359988@pseudo>
  References: <alpine.DEB.2.22.394.2105241211230.165418@pseudo> <CAJ7c6TM2qJdtehyLA79gFtZ+h7jwRrksK4ZAW7boxgQJqQkRAw@mail.gmail.com> <alpine.DEB.2.22.394.2105241509200.165418@pseudo> <699567.1625156441@sss.pgh.pa.us> <alpine.DEB.2.22.394.2107011842240.2143494@pseudo> <746421.1625164869@sss.pgh.pa.us> <CAEZATCXvzDSNhH2uRMUt5K_vmMBDrvo+D7Sn66--1Z_ELz==5Q@mail.gmail.com> <alpine.DEB.2.22.394.2107012212500.2248546@pseudo> <CAEZATCVpnOjrwU=4712OPjNDqV2sVkkK7XHDiR-nzYDL8N6z=Q@mail.gmail.com> <alpine.DEB.2.22.394.2107022340250.2359988@pseudo> <alpine.DEB.2.22.394.2107030902400.2359988@pseudo> <CAEZATCW76GZ-uA-i5_EL4ZukZuvWE_tTNPgRsPkR=0fyiiztTA@mail.gmail.com> <alpine.DEB.2.22.394.2107041115130.2359988@pseudo> <CAEZATCXcmccPQismOioD5kr2NMyMoeLpDaesizzoKh8M-BK5Dw@mail.gmail.com> <alpine.DEB.2.22.394.2107041718570.2359988@pseudo> <CAEZATCWnrVACs2FM219dSkh1ivic8KiGkPEVPtu-MO3nCOYm7A@mail.gmail.com> <alpine.DEB.2.22.394.2107042204240.2359988@pseudo>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox