Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 3 Feb 2003 00:17:35 +1100
From:      Tim Robbins <tjr@FreeBSD.org>
To:        "Andrey A. Chernov" <ache@nagual.pp.ru>
Cc:        Kris Kennaway <kris@obsecurity.org>, current@FreeBSD.org
Subject:   Re: rand() is broken
Message-ID:  <20030203001735.A30440@dilbert.robbins.dropbear.id.au>
In-Reply-To: <20030202123035.GB62977@nagual.pp.ru>; from ache@nagual.pp.ru on Sun, Feb 02, 2003 at 03:30:35PM %2B0300
References:  <20030202070644.GA9987@rot13.obsecurity.org> <20030202090422.GA59750@nagual.pp.ru> <20030202091106.GA72723@rot13.obsecurity.org> <20030202102621.GA60900@nagual.pp.ru> <20030202123035.GB62977@nagual.pp.ru>

next in thread | previous in thread | raw e-mail | index | archive | help
On Sun, Feb 02, 2003 at 03:30:35PM +0300, Andrey A. Chernov wrote:

> On Sun, Feb 02, 2003 at 13:26:21 +0300, Andrey A. Chernov wrote:
> 
> > Workaround I find so far is something like that
> > 
> > #define MASK 123459876
> 
> I found nothing better. Here is fix for 0 problem I plan to commit:
> 
> --- stdlib/rand.c.old	Sat Jan  4 20:39:19 2003
> +++ stdlib/rand.c	Sun Feb  2 14:43:42 2003
> @@ -70,14 +70,18 @@
>   * Park and Miller, Communications of the ACM, vol. 31, no. 10,
>   * October 1988, p. 1195.
>   */
> +#define SHIFT_MASK 123459876
>  	long hi, lo, x;
>  
> -	hi = *ctx / 127773;
> -	lo = *ctx % 127773;
> +	/* Can't be initialized with 0, so use shifting mask. */
> +	x = *ctx ^ SHIFT_MASK;
> +	hi = x / 127773;
> +	lo = x % 127773;
>  	x = 16807 * lo - 2836 * hi;
> -	if (x <= 0)
> +	if (x < 0)
>  		x += 0x7fffffff;
> -	return ((*ctx = x) % ((u_long)RAND_MAX + 1));
> +	*ctx = x ^ SHIFT_MASK;
> +	return (x % ((u_long)RAND_MAX + 1));
>  #endif  /* !USE_WEAK_SEEDING */
>  }

I believe that this change just moves the "bad" seed to 123459876; after
calling srand() with that seed, each call to rand() returns 0.


Tim

To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-current" in the body of the message




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