Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1VX8eK-00039i-8q for pgsql-bugs@arkaria.postgresql.org; Fri, 18 Oct 2013 11:55:24 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1VX8eJ-00041a-Jt for pgsql-bugs@arkaria.postgresql.org; Fri, 18 Oct 2013 11:55:23 +0000 Received: from makus.postgresql.org ([2001:4800:7903:4::125]) by malur.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1VX8eH-00041R-I0 for pgsql-bugs@postgresql.org; Fri, 18 Oct 2013 11:55:21 +0000 Received: from mx1.redhat.com ([209.132.183.28]) by makus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1VX8e8-0007PL-MG for pgsql-bugs@postgresql.org; Fri, 18 Oct 2013 11:55:18 +0000 Received: from int-mx09.intmail.prod.int.phx2.redhat.com (int-mx09.intmail.prod.int.phx2.redhat.com [10.5.11.22]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id r9IBtBbP029261 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Fri, 18 Oct 2013 07:55:11 -0400 Received: from hhorak-t510.usersys.redhat.com (unused-4-233.brq.redhat.com [10.34.4.233]) by int-mx09.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id r9IBtAit002642 for ; Fri, 18 Oct 2013 07:55:10 -0400 Message-ID: <5261219D.3060205@redhat.com> Date: Fri, 18 Oct 2013 13:55:09 +0200 From: Honza Horak User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130923 Thunderbird/17.0.9 MIME-Version: 1.0 To: pgsql-bugs@postgresql.org Subject: random() generates collisions too early Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Scanned-By: MIMEDefang 2.68 on 10.5.11.22 X-Pg-Spam-Score: -7.4 (-------) 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 Hi guys, after playing a bit with "select random();", it turned out that numbers get repeated quite early in a sequence. Originally I set lower PID range (echo "2048" >/proc/sys/kernel/pid_max), but it doesn't seem to affect the results. So, what I observed... First, I generated a set including 1000 randomly generated numbers without setting a seed. touch numbers for i in {1..1000} ; do echo "select random();"|psql|head -n 3|tail -n 1 >>numbers done Then, I continued in generating random numbers and tried to find the new one in the set: for i in {1..10000} ; do if grep `echo "select random();"|psql|head -n 3|tail -n 1` numbers ; then echo "SUCCESS: $i" ; break fi done To my surprise I'm able to find a collision very quickly, in first 1000 numbers usually. Originally, I used psql calls to get random() value from different processes on purpose, but it seems Noah got similar results when random() is called in one process: On 10/18/2013 02:10 AM, Noah Misch wrote: > sudo sysctl -w kernel.pid_max=2048 > psql -c 'create unlogged table samp(c float8)' > for n in `seq 1 200000`; do psql -qc 'insert into samp values (random())'; done > > 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. As I was told this is not taken as a security issue, since random() is not considered as a CSPRNG in any case, but as Noah said, we should probably try to make it a bit better. Also, I'd suggest to state explicitly in the doc, that random() shouldn't be taken as CSPRNG, since I can imagine people blindly believing that random() can be good enough for such use cases, just because they see how many possible values they get from double-precision type: http://www.postgresql.org/docs/9.3/static/functions-math.html Regards, Honza -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs