amd64/121885: random() system call returning the same number
on amd64
Lewis
lewis at elasticmind.net
Thu Mar 20 11:30:03 UTC 2008
The following reply was made to PR amd64/121885; it has been noted by GNATS.
From: Lewis <lewis at elasticmind.net>
To: Roland Smith <rsmith at xs4all.nl>
Cc: FreeBSD-gnats-submit at freebsd.org
Subject: Re: amd64/121885: random() system call returning the same number
on amd64
Date: Thu, 20 Mar 2008 11:03:28 +0000
Hi,
Thanks for looking at this and spotting the problem, so much time was
wasted trying to figure out what was wrong :( I guess much of the
confusion was caused by the same code working on i386 but not amd64 and
is what prompted people to suggest to me writing a PR. Would it have
been because amd64 uses different sizes of 'double' than i386 or
something like that?
Regards,
Lewis.
Roland Smith wrote:
> On Wed, Mar 19, 2008 at 10:02:31PM +0000, Lewis wrote:
>
>> --- 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;
>>
>
> This should be 'long random;', see random(3).
>
> With this change it works. Or, see below.
>
>
>> 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);
>>
>
> And if you want to use doubles, you should have used '%f' of '%g' here,
> instead of '%u'. In which case it works as well.
>
>
>> }
>> }
>> }
>>
>> /* Sample program output:
>>
> (long r_num):
> time: 1205997021
> pid: 59547
> random: 1651842516 - Sent packet.
> random: 658819253 - Sent packet.
> random: 597332715 - Sent packet.
> random: 635700682 - Sent packet.
> random: 996321444 - Sent packet.
> random: 870878043 - Sent packet.
> random: 1977054922 - Sent packet.
> random: 955816479 - Sent packet.
> random: 367175873 - Sent packet.
> random: 16441391 - Sent packet.
> random: 1188837559 - Sent packet.
> random: 1906346020 - Sent packet.
> random: 151679052 - Sent packet.
>
> (double r_num, with '%g' in printf)
> time: 1205997420
> pid: 59748
> random: 9.87536e+08 - Sent packet.
> random: 1.5497e+09 - Sent packet.
> random: 2.13094e+09 - Sent packet.
> random: 1.08434e+09 - Sent packet.
> random: 1.94301e+09 - Sent packet.
> random: 1.34453e+09 - Sent packet.
> random: 1.77993e+09 - Sent packet.
> random: 1.10691e+09 - Sent packet.
> random: 1.95755e+09 - Sent packet.
> random: 1.70928e+09 - Sent packet.
> random: 1.66913e+09 - Sent packet.
> random: 1.90308e+09 - Sent packet.
>
> I think this PR can be closed.
>
> Roland
>
More information about the freebsd-amd64
mailing list