In fact, I just wrapped this up into my Mersenne twister code - randInt
is now built in to the library and I eliminated the function call
overhead of calling rand() (or the mersenne equivalent.)  Probably won't
make a noticeable difference, but since I'm porting it to D anyway, I
might as well fix this.

- Don


On Thu, 2006-12-07 at 12:12 -0500, Don Dailey wrote:
> On Thu, 2006-12-07 at 16:05 +0100, Ɓukasz Lew wrote:
> >     ii = pm::rand () % empty_v_cnt;         // TODO improve speed "%"
> 
> 
> Try this,  I think it could be faster, not sure,  but has the advantage
> that it's slightly more correct.
> 
> // returns an integer between 0 and n-1 inclusive
> // 
> unsigned long randint(unsigned long n) 
> {
>   unsigned long   v =  n;
>   unsigned long   r;
> 
>   v--;
>   v |= v >> 1;
>   v |= v >> 2;
>   v |= v >> 4;
>   v |= v >> 8;
>   v |= v >> 16;
> 
>   do  { r = rand(); } while ( (r & v) >= n );
> 
>   return( r & v );
> }
> 
> 
> 
> - Don
> 
> 
> _______________________________________________
> computer-go mailing list
> computer-go@computer-go.org
> http://www.computer-go.org/mailman/listinfo/computer-go/

_______________________________________________
computer-go mailing list
computer-go@computer-go.org
http://www.computer-go.org/mailman/listinfo/computer-go/

Reply via email to