Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1VZLWI-0006YT-T5 for pgsql-bugs@arkaria.postgresql.org; Thu, 24 Oct 2013 14:04:15 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1VZLWI-0001Fp-8b for pgsql-bugs@arkaria.postgresql.org; Thu, 24 Oct 2013 14:04:14 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1VZLWF-0001Bu-K5 for pgsql-bugs@postgresql.org; Thu, 24 Oct 2013 14:04:11 +0000 Received: from smtp-outbound-2.vmware.com ([208.91.2.13]) by magus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1VZLWB-00032r-3P for pgsql-bugs@postgresql.org; Thu, 24 Oct 2013 14:04:11 +0000 Received: from sc9-mailhost2.vmware.com (sc9-mailhost2.vmware.com [10.113.161.72]) by smtp-outbound-2.vmware.com (Postfix) with ESMTP id 94E2298576; Thu, 24 Oct 2013 07:04:03 -0700 (PDT) Received: from zcs-prod-ext-mta-1.vmware.com (zcs-prod-ext-mta-1.vmware.com [10.113.62.223]) by sc9-mailhost2.vmware.com (Postfix) with ESMTP id 919F2B0592; Thu, 24 Oct 2013 07:04:03 -0700 (PDT) Received: from zcs-prod-ext-mta-1.vmware.com (localhost.localdomain [127.0.0.1]) by zcs-prod-ext-mta-1.vmware.com (Postfix) with ESMTP id 66B4D12002F; Thu, 24 Oct 2013 07:11:06 -0700 (PDT) Received: from [192.168.1.90] (zimbra-prod-ext-proxy-vip.vmware.com [10.113.63.87]) by zcs-prod-ext-mta-1.vmware.com (Postfix) with ESMTPSA; Thu, 24 Oct 2013 07:11:05 -0700 (PDT) Message-ID: <526928D0.6090707@vmware.com> Date: Thu, 24 Oct 2013 17:04:00 +0300 From: Heikki Linnakangas Organization: VMware User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:10.0.12) Gecko/20130116 Icedove/10.0.12 MIME-Version: 1.0 To: Honza Horak CC: pgsql-bugs@postgresql.org, Tom Lane Subject: Re: random() generates collisions too early References: <5261219D.3060205@redhat.com> <526537ED.2010607@vmware.com> <29392.1382368446@sss.pgh.pa.us> <5266726E.5030300@redhat.com> In-Reply-To: <5266726E.5030300@redhat.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Pg-Spam-Score: -7.3 (-------) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-bugs Precedence: bulk Sender: pgsql-bugs-owner@postgresql.org On 22.10.2013 15:41, Honza Horak wrote: > On 10/21/2013 05:14 PM, Tom Lane wrote: >> Heikki Linnakangas writes: >>> On 18.10.2013 14:55, Honza Horak wrote: >>>> The results covered only 181383 distinct values, and 68 values >>>> repeated four or five times each. We should at least consider using a >>>> higher-entropy seed. >> >>> Interesting. PostgreSQL's random() function just calls the underlying >>> libc random() function. I assume you tested this on with Linux and >>> glibc. >> >> I agree with the theory that this probably isn't the fault of the >> random() >> function as such, but with our code to reset the random seed when forking >> a postmaster child process. Note that the test case is only examining the >> first random value created in each process. So basically what this is >> measuring is the number of different seed values we use. >> >> regards, tom lane >> > > After playing a bit more with random() value and the seed defined in > src/backend/postmaster/postmaster.c:4038 it seems really like the seed > doesn't have very good characteristic. The PID number used there ensures > the value to be different in child processes, but when only xor-ed with > usecs, it doesn't enlarge the total data domain of the seed, which stays > 1M values. Then having in mind the birthday paradox and probably not > ideal PRNG, it doesn't seem unrealistic to get two same random numbers > in only hundreds/thousands of tries. > > In order to enlarge possible data domain of the seed we can include sec > count as well and shift some xor-ed variables to use the whole range or > unsigned int. The attached patch uses a way that gave me much better > results (collision found approx. after a hundred thousands of values): > > - srandom((unsigned int) (MyProcPid ^ usecs)); > + srandom((unsigned int) (MyProcPid ^ (usecs << 12) ^ secs)); Seems reasonable, committed. Thanks! - Heikki -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs