Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 27 May 2005 11:26:27 -0400
From:      John Baldwin <jhb@FreeBSD.org>
To:        freebsd-amd64@FreeBSD.org, Adriaan de Groot <groot@kde.org>
Subject:   Re: amd64/81279: /usr/games/random returns every line
Message-ID:  <200505271126.28696.jhb@FreeBSD.org>
In-Reply-To: <200505192130.j4JLU7wg078285@freefall.freebsd.org>
References:  <200505192130.j4JLU7wg078285@freefall.freebsd.org>

next in thread | previous in thread | raw e-mail | index | archive | help
On Thursday 19 May 2005 05:30 pm, Adriaan de Groot wrote:
> The following reply was made to PR amd64/81279; it has been noted by GNATS.
>
> From: Adriaan de Groot <groot@kde.org>
> To: bug-followup@freebsd.org, vivek@khera.org
> Cc:
> Subject: Re: amd64/81279: /usr/games/random returns every line
> Date: Thu, 19 May 2005 23:26:08 +0200
>
>  Problem is lines like
>
>          selected = (int)(denom * random() / LONG_MAX) == 0;
>
>  which cause the overflow. Replacing LONG_MAX with INT_MAX here fixes the
> bug. (The same replacement should probably be done in the other two places
> that LONG_MAX occurs, since the -e flag is similarly broken.)

I can't see how the overflow happens.  Oh.  From the manpage:

DESCRIPTION
     The random() function uses a non-linear additive feedback random number
     generator employing a default table of size 31 long integers to return
     successive pseudo-random numbers in the range from 0 to (2**31)-1.  The
     period of this random number generator is very large, approximately
     16*((2**31)-1).

So I guess the real fix is to replace LONG_MAX with RAND_MAX?

-- 
John Baldwin <jhb@FreeBSD.org>  <><  http://www.FreeBSD.org/~jhb/
"Power Users Use the Power to Serve"  =  http://www.FreeBSD.org



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?200505271126.28696.jhb>