From owner-freebsd-stable@FreeBSD.ORG Mon Apr 7 07:01:20 2003 Return-Path: Delivered-To: freebsd-stable@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 99EE537B401 for ; Mon, 7 Apr 2003 07:01:20 -0700 (PDT) Received: from sccrmhc02.attbi.com (sccrmhc02.attbi.com [204.127.202.62]) by mx1.FreeBSD.org (Postfix) with ESMTP id E969C43FCB for ; Mon, 7 Apr 2003 07:01:17 -0700 (PDT) (envelope-from timothy@voidnet.com) Received: from repose (12-210-146-224.client.attbi.com[12.210.146.224]) by sccrmhc02.attbi.com (sccrmhc02) with SMTP id <20030407140117002004l5q8e>; Mon, 7 Apr 2003 14:01:17 +0000 From: Eric Timme (by way of Eric Timme ) Date: Mon, 7 Apr 2003 09:01:20 -0500 User-Agent: KMail/1.5 To: freebsd-stable@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit Content-Disposition: inline Message-Id: <200304070901.20557.timothy@voidnet.com> Subject: 4-stable and C rand()? X-BeenThere: freebsd-stable@freebsd.org X-Mailman-Version: 2.1.1 Precedence: list List-Id: Production branch of FreeBSD source code List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 07 Apr 2003 14:01:20 -0000 Hi everyone, sorry if this has been answered before - I caught a whiff of a discussion about c's rand() function in a mailing list archive, but couldn't find a definitive answer. I'm trying to do a simple CS project on my machine where I generate two sets of values in parallel using rand() and am running into infinite loops of values, and couldn't figure out why, so I wrote a test program: #include #include #include int main() { int a, b; srand((unsigned)time(0)); for (int i=0 ; i < 50 ; i++) cout << rand() % 32 << " " << rand() % 4 << endl; return 0; } No matter how many times I run this it seems to alternate between generating two different but non-unique sets of values, depending on whether time(0) is even or odd..and I can't understand why (values at the end of this message). My uname -a is: FreeBSD repose 4.8-RC FreeBSD 4.8-RC #0: Mon Mar 24 09:13:35 CST 2003 timothy@repose:/usr/obj/usr/src/sys/Repose i386 It seems like the consensus on the thread I read was to use /dev/random or /dev/urandom..but this isn't really an option for a CS project a TA has to grade, so I've had to SSH out to finish the project. I just want to know what I'm doing wrong, and why I can't do this on my FreeBSD machine =\ The two value loops are: 22 1 4 3 2 1 16 3 14 1 28 3 26 1 8 3 6 1 20 3 18 1 0 3 30 1 12 3 10 1 24 3 22 1 4 3 2 1 16 3 14 1 28 3 26 1 8 3 6 1 20 3 18 1 0 3 and 19 2 9 0 15 2 5 0 11 2 1 0 7 2 29 0 3 2 25 0 31 2 21 0 27 2 17 0 23 2 13 0 19 2 9 0 15 2 5 0 11 2 1 0 7 2 29 0 3 2 25 0 31 2 21 0 27 2 17 0 23 2 13 0 19 2 Thanks for any insight you can lend.