looking for the easiest way to generate random numbers ranging from [-64 63] using rand().
can't seem to hack it.
This is a discussion on rand interval within the C++ Programming forums, part of the General Programming Boards category; looking for the easiest way to generate random numbers ranging from [-64 63] using rand(). can't seem to hack it....
looking for the easiest way to generate random numbers ranging from [-64 63] using rand().
can't seem to hack it.
rand()%128 - 64
If I have eight hours for cutting wood, I spend six sharpening my axe.
vart's solution works, but you might also want to read more: using rand()
C + C++ Compiler: MinGW port of GCC
Version Control System: Bazaar
Look up a C++ Reference and learn How To Ask Questions The Smart Way
Or "gasp, shock, horror", the FAQ
http://faq.cprogramming.com/cgi-bin/...&id=1043284385
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
I support http://www.ukip.org/ as the first necessary step to a free Europe.
It was provided becausevart's solution works
N is 128 in this case and the condition should be fulfilled...The first solution only works when N evenly divides into RAND_MAX
If I have eight hours for cutting wood, I spend six sharpening my axe.
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.
>N is 128 in this case and the condition should be fulfilled...
Who said RAND_MAX has to be a power of two?
My best code is written with the delete key.