[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [patches] strfry() doesn't use a level random distribution



On Sat, May 09, 2009 at 09:09:11AM -0500, Kirk Strauser wrote:
> Index: strfry.c
> ===================================================================
> --- strfry.c	(revision 8417)
> +++ strfry.c	(working copy)
> @@ -41,8 +41,14 @@
>      for (size_t i = 0; i < len - 1; ++i)
>        {
>  	int32_t j;
> -	__random_r (&rdata, &j);
> -	j = j % (len - i) + i;
> +        int32_t randmax = 2^31 - 1;
> +        randmax = randmax - randmax % (len - i);
> +        do
> +          {
> +	    __random_r (&rdata, &j);
> +	    j = j % (len - i);
> +          } while (j >= randmax);
> +        j = j + i;
>
>  	char c = string[i];
>  	string[i] = string[j];

  By the way, your patch is whitespace-damanged.

-- 
				Petr "Pasky" Baudis
The lyf so short, the craft so long to lerne. -- Chaucer