View Single Post
  #3  
Old 03-19-2010, 07:34 PM
Matt L Matt L is offline
Registered User
 
Join Date: Oct 2005
Posts: 4,263
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().
Reply With Quote