Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1YDDAf-000832-9e for pgsql-bugs@arkaria.postgresql.org; Mon, 19 Jan 2015 14:19:13 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1YDDAe-0001a9-8s for pgsql-bugs@arkaria.postgresql.org; Mon, 19 Jan 2015 14:19:12 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1YCu0G-0002Zc-7J for pgsql-bugs@postgresql.org; Sun, 18 Jan 2015 17:51:12 +0000 Received: from wrigleys.postgresql.org ([204.145.124.246]) by magus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1YCu06-0003Vl-S4 for pgsql-bugs@postgresql.org; Sun, 18 Jan 2015 17:51:10 +0000 MIME-Version: 1.0 Subject: BUG #12589: Poor randomness from random() with some seeds; poor resolution To: pgsql-bugs@postgresql.org From: pgsql-004@personal.formauri.es Date: Sun, 18 Jan 2015 17:50:56 +0000 Message-ID: <20150118175056.1983.60420@wrigleys.postgresql.org> X-Pg-Spam-Score: -1.9 (-) Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit 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 The following bug has been logged on the website: Bug reference: 12589 Logged by: Pedro Gimeno Email address: pgsql-004@personal.formauri.es PostgreSQL version: 9.1.13 Operating system: Debian Wheezy Description: postgres=> select version(); version ---------------------------------------------------------------------------------------- PostgreSQL 9.1.13 on i686-pc-linux-gnu, compiled by gcc (Debian 4.7.2-5) 4.7.2, 32-bit (1 row) postgres=> select setseed(1); setseed --------- (1 row) postgres=> -- formatted as array for compactness of output in email postgres=> select array(select floor(random()*40) from generate_series(1, 40)); ?column? --------------------------------------------------------------------------------------------------------------------------- {19,39,19,19,39,19,39,39,39,39,19,19,19,19,39,39,39,39,19,39,39,39,39,19,39,19,39,19,39,19,39,19,19,19,39,19,38,38,19,38} (1 row) postgres=> select setseed(-1); setseed --------- (1 row) postgres=> select array(select floor(random()*40) from generate_series(1, 40)); ?column? ----------------------------------------------------------------------------------------------------- {20,0,20,20,0,20,0,0,0,0,20,20,20,20,0,0,0,0,20,0,0,0,0,20,0,20,0,20,0,20,0,20,20,20,0,20,1,1,20,1} (1 row) It gets somewhat better as things progress, but the start of the sequence exhibits very clear non-randomness. The cause seems to be GNU libc: $ cat x.c #include #include main() { int i; srandom(0x7FFFFFFF); for (i = 0; i < 40; i++) printf(i?",%d":"%d", (random()/(RAND_MAX/40))); puts(""); } $ make x cc x.c -o x $ ./x 19,39,19,19,39,19,39,39,39,39,19,19,19,19,39,39,39,39,19,39,39,39,39,19,39,19,39,19,39,19,39,19,19,19,39,19,38,38,19,38 $ dpkg -l libc6 | grep libc6 ii libc6:i386 2.17-3 i386 Embedded GNU C Library: Shared libraries PostgreSQL should probably use its own pseudo-random number generator and not depend on the system's, as there's scholar literature (e.g. Knuth's TAOCP) indicating that many systems' default generator has poor quality. Any replacement PRNG should preferably be cross-platform compatible, thus providing sequence repeatability across platforms, modulo float rounding errors. A fast and more reliable (non-crypto) PRNG such as Mersenne Twister or WELL is not hard to implement and they tend to be fast enough for most purposes. On top on that, the returned value does not really have full float8 precision: postgres=> select mod((random()*2147483648)::numeric(14,4), 1); mod -------- 0.0000 (1 row) (the latter output happens always no matter how many times it's tried). -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs