looking for the easiest way to generate random numbers ranging from [-64 63] using rand().

can't seem to hack it.

Printable View

- 12-02-2006jarheadhalorand interval
looking for the easiest way to generate random numbers ranging from [-64 63] using rand().

can't seem to hack it. - 12-02-2006vart
rand()%128 - 64

- 12-02-2006laserlight
vart's solution works, but you might also want to read more: using rand()

- 12-02-2006Salem
Or "gasp, shock, horror", the FAQ

http://faq.cprogramming.com/cgi-bin/...&id=1043284385 - 12-02-2006vartQuote:

vart's solution works

Quote:

The first solution only works when N evenly divides into RAND_MAX

- 12-02-2006Daved
You never answered my question from the last thread. There's a real possibility that you want unique numbers, and getting a random number in the range [-63, 64] is the wrong way to go about that.

- 12-02-2006Prelude
>N is 128 in this case and the condition should be fulfilled...

Who said RAND_MAX has to be a power of two?