Ricardo Mestre wrote:
> While checking if there was still a score file on trek(6) I found that
> it was still using random(), and  inspired by the code on NetBSD and
> OpenBSD relevant manpages I came up with this diff. Let's bring
> trek(6) to the 21st century, at least here :) Does it look good?
> 
> [...]
> 
> Index: ranf.c
> ===================================================================
> RCS file: /cvs/src/games/trek/ranf.c,v
> retrieving revision 1.5
> diff -u -p -u -r1.5 ranf.c
> --- ranf.c    27 Oct 2009 23:59:27 -0000      1.5
> +++ ranf.c    25 Nov 2015 20:19:47 -0000
> @@ -33,23 +33,17 @@
>  #include <stdio.h>
>  #include <stdlib.h>
>  
> -int
> -ranf(max)
> -     int     max;
> +uint32_t
> +ranf(uint32_t max)
>  {
> -     int     t;
> -
>       if (max <= 0)
>               return (0);
> -     t = random() >> 5;
> -     return (t % max);
> +     return (arc4random_uniform(max));
>  }
>  
>  

Why not just replace ranf with arc4random_uniform? That condition looks
like a nop because max is unsigned and arc4random_uniform returns 0 when
its arg is < 2. So this patch makes ranf an alias of arc4random_uniform.

Also:

 * what was the point of the shift-right-by-five in the original code?

 * are you sure that changing the type of this function doesn't have any
   downstream effects?

I like this idea, though.

Reply via email to