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 11:35:09 +0200 (CEST)
Message-ID: <alpine.DEB.2.22.394.2107041115130.2359988@pseudo> (raw)
In-Reply-To: <CAEZATCW76GZ-uA-i5_EL4ZukZuvWE_tTNPgRsPkR=0fyiiztTA@mail.gmail.com>
References: <alpine.DEB.2.22.394.2105241211230.165418@pseudo>
	<43a6e30b-542f-671a-e7bb-b7f78cf90e8c@enterprisedb.com>
	<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>


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.





view thread (59+ messages)  latest in thread

Message-ID: <alpine.DEB.2.22.394.2107041115130.2359988@pseudo>
Permalink:  ../alpine.DEB.2.22.394.2107041115130.2359988@pseudo/
Also on:    postgresql.org/message-id/alpine.DEB.2.22.394.2107041115130.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.2107041115130.2359988@pseudo>
  References: <alpine.DEB.2.22.394.2105241211230.165418@pseudo> <43a6e30b-542f-671a-e7bb-b7f78cf90e8c@enterprisedb.com> <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>

* 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