Click here to Skip to main content
16,020,261 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need to generate number between 4 to 33, how can I do this using rand() method?

Regards,
Joy
Posted
Comments
Mohibur Rashid 3-Aug-15 4:39am    
What have you tried?

This:
C++
int randNum = rand()%(max-min + 1) + min;

would do the trick ;)
Where min = 4 and max = 33
 
Share this answer
 
Comments
CPallini 3-Aug-15 4:44am    
5.
Maciej Los 3-Aug-15 4:50am    
Thank you, Carlo.
[no name] 3-Aug-15 9:10am    
Looks good, 5.
Maciej Los 3-Aug-15 9:11am    
Thank you
As an alternative, if you have a modern compiler, you might consider using the new random number generation facilities: <random>[^]
 
Share this answer
 
Comments
Maciej Los 3-Aug-15 5:01am    
5ed!
CPallini 3-Aug-15 5:17am    
Thank you.
[no name] 3-Aug-15 9:11am    
Looks good, 5.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900