ahmed sobhy Posted February 2, 2012 Posted February 2, 2012 I am trying to generate a random number between i and j. The code at the bottom always generates the same number which is 42. How can I fix this ? int RandInt (int i , int j) { return rand( ) % (j-i+1) + i ; }
Cap'n Refsmmat Posted February 2, 2012 Posted February 2, 2012 Is this C? Have you seeded the random number generator before using it? If you don't, it'll be seeded with the same value each time you run the program, and it will generate the same sequence of random numbers every time.
ahmed sobhy Posted February 2, 2012 Author Posted February 2, 2012 it is c++ , can you help with the code ?
Cap'n Refsmmat Posted February 2, 2012 Posted February 2, 2012 As I said, have you seeded the random number generator?
ahmed sobhy Posted February 2, 2012 Author Posted February 2, 2012 no I didn't. How can I do it using the c++?
Cap'n Refsmmat Posted February 2, 2012 Posted February 2, 2012 http://www.cplusplus.com/reference/clibrary/cstdlib/srand/
John Cuthber Posted February 2, 2012 Posted February 2, 2012 Optional Dilbert cartoon. http://dilbert.com/strips/comic/2001-10-25/
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now