View Single Post
  #4  
Old 03-19-2010, 09:45 PM
TheDon's Avatar
TheDon TheDon is offline
Ghost of Diesels Past
 
Join Date: Oct 2005
Posts: 13,285
Quote:
Originally Posted by Matt L View Post
Yes, you can use rand()%6 to get a number in the range of [0,5].

For any purists out there, I will note that as this uses only the low bits of the result of rand(), it is not very random. You should be using the high bits by dividing by RAND_MAX in floating-point, multiply by 6, then convert to an integer.

Also note that the modulus operator is well-defined on positive inputs only. That's not something to worry about with the return value of rand().
I could do RAND_MAX deal ... could..
Reply With Quote