April 16, 2006
Charles wrote:

> Charles wrote:
> 
>> How do i get a number in the range -0.5 to 0.5 ?
> 
> To Clarify:
> 
> How do i get a random real in the range -0.5 to 0.5 using std.random ?
> 
> -- 
> 
> Even using the traditional C method , using std.c.stdlib, using int.max for RAND_MAX scince its not defined is not working ->

That's because std.random.rand's range is 0 to uint.max, not 0 to int.max.

> const int RAND_MAX = int.max;
> float x = (rand() / (RAND_MAX + 1.0)) - 0.5;
> 
> This always yeilds numbers of ( -0.4999XX , where XX is from 80 99 ).
<snip>

Very weird.  When I try it, I get the range -0.5 to 1.5 as expected.

If OTOH I use uint.max instead of RAND_MAX, then I get what you're asking for.

Stewart.
1 2
Next ›   Last »