Date: Wed, 19 Mar 2008 22:02:31 +0000 (GMT) From: Lewis <moggie@elasticmind.net> To: FreeBSD-gnats-submit@FreeBSD.org Subject: amd64/121885: random() system call returning the same number on amd64 Message-ID: <20080319220231.7639B26D025@mail.elasticmind.net> Resent-Message-ID: <200803192230.m2JMU1ag049502@freefall.freebsd.org>
next in thread | raw e-mail | index | archive | help
>Number: 121885 >Category: amd64 >Synopsis: random() system call returning the same number on amd64 >Confidential: no >Severity: serious >Priority: medium >Responsible: freebsd-amd64 >State: open >Quarter: >Keywords: >Date-Required: >Class: sw-bug >Submitter-Id: current-users >Arrival-Date: Wed Mar 19 22:30:00 UTC 2008 >Closed-Date: >Last-Modified: >Originator: Lewis >Release: FreeBSD 6.2-RELEASE-p11 amd64 >Organization: >Environment: System: FreeBSD 6.2-RELEASE-p11 FreeBSD 6.2-RELEASE-p11 #1: Sat Feb 16 11:59:02 GMT 2008 amd64 Also: FreeBSD 6.3-RELEASE-p1 FreeBSD 6.3-RELEASE-p1 #0: Sun Feb 17 23:38:18 GMT 2008 amd64 gcc -v Using built-in specs. Configured with: FreeBSD/amd64 system compiler Thread model: posix gcc version 3.4.6 [FreeBSD] 20060305 >Description: I was experiencing some strange behavour using random() in a program so I thought I'd better make a test program to see what was going on. The test program is simply trying to use some random numbers to simulate packet loss. Unfortunately when the test program executed it showed the random() number function returning the number '8440544' each time. It first appeared that there was something wrong with my program, but when it was executed on an i386 box it seemed to work as expected - producing seemingly random numbers. I ran the program on another amd64 machine which behaved in the same way as the first test, resulting in random() returning the same number each time. The source code for the test program is included below. >How-To-Repeat: The test program was just compiled as any other and executed: gcc -o random random.c ./random >Fix: --- random.c begins here --- #include <stdio.h> #include <strings.h> #include <stdlib.h> #include <time.h> #include <sys/types.h> #include <unistd.h> int main(int argc, char ** argv) { srandom(time(0) * getpid()); printf("time: %d\n", time(0)); printf("pid: %d\n", getpid()); double r_num; int i, percent = 50; /* Constant 0x7fffffff is equal to (2**31)-1, which is the * maximum value returned by the random() number function. * http://web.mit.edu/answers/c/c_random_numbers.html 2008/03/19 */ for (i = 0; i < 100; i++) { /*r_num = ((float) random() / (float) 0x7fffffff);*/ r_num = random(); if (r_num < (percent / 100)) { printf("random: %u - Dropped packet.\n",r_num); } else { printf("random: %u - Sent packet.\n", r_num); } } } /* Sample program output: random: 8440544 - Sent packet. random: 8440544 - Sent packet. random: 8440544 - Sent packet. random: 8440544 - Sent packet. random: 8440544 - Sent packet. */ --- random.c ends here --- >Release-Note: >Audit-Trail: >Unformatted:
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20080319220231.7639B26D025>