why the array bounds array

BCS ao at pathlink.com
Mon Dec 8 10:07:25 PST 2008


Reply to Sergey,

> Mon, 8 Dec 2008 06:59:40 +0000 (UTC), BCS wrote:
>>
>>>> better (I think):
>> rand() / (RAND_MAX / TYPES_OF_TILES)
>> 


> Don't use rand() like this, ever.  ;)  "RAND_MAX / TYPES_OF_TILES" is
> an integer expression, it rounds down, therefore your formula gives a
> slightly greater range of values than you expect.  You'll get all
> sorts of out of bounds errors.
>

thus the (I think). I was just to lazy to figure out the rounding.

the idea still work as long as you round up.

rand() / ( RAND_MAX / TYPES_OF_TILES + !!(RAND_MAX % TYPES_OF_TILES))

OTOH using someone else's code that does the same thing is always better.
 
> Your best bet is to use a uniform distribution method of the same
> random number generator implementation.  There is std.random.uniform()
> in D2's Phobos which serves this purpose.  If you don't have an
> equivalent, your next stop is "rand() % TYPES_OF_TILES".  It's not
> strictly uniform but close if TYPES_OF_TILES is small.

if TYPES_OF_TILES is to small (like 4) it start s getting highly un random 
again.
> 




More information about the Digitalmars-d-learn mailing list