From owner-freebsd-questions Sat Nov 18 05:52:56 1995 Return-Path: owner-questions Received: (from root@localhost) by freefall.freebsd.org (8.6.12/8.6.6) id FAA25308 for questions-outgoing; Sat, 18 Nov 1995 05:52:56 -0800 Received: from iaehv.IAEhv.nl (root@iaehv.IAEhv.nl [192.87.208.2]) by freefall.freebsd.org (8.6.12/8.6.6) with ESMTP id FAA25297 for ; Sat, 18 Nov 1995 05:52:49 -0800 Received: from oasis.IAEhv.nl by iaehv.IAEhv.nl (8.6.12/1.63) id OAA18302; Sat, 18 Nov 1995 14:52:44 +0100 X-Disclaimer: iaehv.nl is a public access UNIX system and cannot be held responsible for the opinions of its individual users. Received: by oasis.IAEhv.nl (8.7.1/1.63) id OAA04737; Sun, 19 Nov 1995 14:51:38 +0100 (MET) From: volf@oasis.IAEhv.nl (Frank Volf) Message-Id: <199511191351.OAA04737@oasis.IAEhv.nl> Subject: What's wrong with this code??? To: freebsd-questions@freebsd.org Date: Sun, 19 Nov 1995 14:51:37 +0100 (MET) X-Mailer: ELM [version 2.4 PL24] Content-Type: text Sender: owner-questions@freebsd.org Precedence: bulk Hi, I have an application that uses random() to generate random numbers. For generating the random numbers I use the following piece of code: #include #include #include #include #include main() { int i; long t; t = time(NULL); srandom((u_int)t); printf("%ld %d %d %d\n",t, random() % 8, random() % 8, random() % 8); } To my surprise the range of numbers generated is not random at all between multiple invocations of the program. In fact the random generator seems to start again after 16 invocations (with 1 second delay between them so t has incremented): 816788556 6 1 1 816788557 5 6 3 816788558 5 3 6 816788559 4 0 0 816788560 4 5 3 816788561 3 2 5 816788563 2 4 2 816788564 2 1 5 816788565 1 6 7 816788566 1 3 2 816788567 0 0 4 816788568 0 5 7 816788569 7 2 1 816788570 7 7 4 816788571 6 4 6 816788572 6 1 1 816788573 5 6 3 816788574 5 3 6 816788575 4 0 0 etc. What is wrong? Is it a bug in my program (maybe the cast?) or is there a problem with the random number generator used in FreeBSD? Regards, Frank ---------------------------------------------------------------------------- Frank Volf - Internet Access Eindhoven - Digitale Stad Eindhoven ---------------------------------------------------------------------------- || volf@oasis.IAEhv.nl - use for personal mail || || volf@IAEhv.nl - use for Internet Access Eindhoven related mail || || volf@dse.dse.nl - use for Digital City of Eindhoven related mail || ---------------------------------------------------------------------------- IAE Public Access Unix System - Dial +31.40.2439436 and login as new. ----------------------------------------------------------------------------