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 1m0IDd-0005sg-F3 for pgsql-hackers@arkaria.postgresql.org; Mon, 05 Jul 2021 06:36:38 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1m0IDc-00063l-AT for pgsql-hackers@arkaria.postgresql.org; Mon, 05 Jul 2021 06:36:36 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1m0IDc-00063d-04 for pgsql-hackers@lists.postgresql.org; Mon, 05 Jul 2021 06:36:36 +0000 Received: from mail.postgrespro.ru ([93.174.131.139]) by magus.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1m0IDV-0000MF-BI for pgsql-hackers@lists.postgresql.org; Mon, 05 Jul 2021 06:36:35 +0000 Received: from mail.postgrespro.ru (cyclops.postgrespro.ru [93.174.131.138]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (Client did not present a certificate) by mail.postgrespro.ru (Postfix) with ESMTPSA id D1E8121C2CDA; Mon, 5 Jul 2021 09:36:27 +0300 (MSK) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=postgrespro.ru; s=mail; t=1625466987; bh=bfuprFX71acce4GPBuvVII875iHJbRC0ZbgOhHWFfUc=; h=Date:From:To:Cc:Subject:In-Reply-To:References; b=BVrxqd8wNKWqedEPoW4xP1mptTa0NArLG3tk8AW+XCIkRv4NbY44KDbVw3HFODcIQ YXm9RCmYg5JBHl14VuaeZ7opVHu7GvT783R5iEOCKoZt3UDC9SXr3NB20KawsB4oWQ 04ttc1TYbMdKh0f9LaGDbmlQ0rT9VYkxqGaor/z4= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Date: Mon, 05 Jul 2021 09:36:27 +0300 From: Yura Sokolov To: Fabien COELHO Cc: Dean Rasheed , Tom Lane , Aleksander Alekseev , PostgreSQL Developers Subject: Re: rand48 replacement In-Reply-To: References: <699567.1625156441@sss.pgh.pa.us> <746421.1625164869@sss.pgh.pa.us> User-Agent: Roundcube Webmail/1.4.0 Message-ID: <3136e2672d5ab395521bae05a80df469@postgrespro.ru> X-Sender: y.sokolov@postgrespro.ru List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Fabien COELHO писал 2021-07-04 23:29: >> 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. I believe most "range" values are small, much smaller than UINT32_MAX. In this case, according to [1] fastest method is Lemire's one (I'd take original version from [2]) Therefore combined method pg_prng_u64_range could branch on range value uint64 pg_prng_u64_range(pg_prng_state *state, uint64 range) { uint64 val = xoroshiro128ss(state); uint64 m; if ((range & (range-1) == 0) /* handle all power 2 cases */ return range != 0 ? val & (range-1) : 0; if (likely(range < PG_UINT32_MAX/32) { /* * Daniel Lamire's unbiased range random algorithm based on rejection sampling * https://lemire.me/blog/2016/06/30/fast-random-shuffling/ */ m = (uint32)val * range; if ((uint32)m < range) { uint32 t = -range % range; while ((uint32)m < t) m = (uint32)xoroshiro128ss(state) * range; } return m >> 32; } /* Apple's mask method */ m = mask_u64(range-1); val &= m; while (val >= range) val = xoroshiro128ss(state) & m; return val; } Mask method could also be faster when range is close to mask. For example, fast check for "range is within 1/1024 to mask" is range < (range/512 + (range&(range*2))) And then method choose could like: if (likely(range < UINT32_MAX/32 && range > (range/512 + (range&(range*2))))) But I don't know does additional condition worth difference or not. [1] https://www.pcg-random.org/posts/bounded-rands.html [2] https://lemire.me/blog/2016/06/30/fast-random-shuffling/ regards, Sokolov Yura