ahmed sobhy Posted February 2, 2012 Share 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 ; } Link to comment Share on other sites More sharing options...
Cap'n Refsmmat Posted February 2, 2012 Share 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. Link to comment Share on other sites More sharing options...
ahmed sobhy Posted February 2, 2012 Author Share Posted February 2, 2012 it is c++ , can you help with the code ? Link to comment Share on other sites More sharing options...
Cap'n Refsmmat Posted February 2, 2012 Share Posted February 2, 2012 As I said, have you seeded the random number generator? Link to comment Share on other sites More sharing options...
ahmed sobhy Posted February 2, 2012 Author Share Posted February 2, 2012 no I didn't. How can I do it using the c++? Link to comment Share on other sites More sharing options...
Cap'n Refsmmat Posted February 2, 2012 Share Posted February 2, 2012 http://www.cplusplus.com/reference/clibrary/cstdlib/srand/ Link to comment Share on other sites More sharing options...
Schrödinger's hat Posted February 2, 2012 Share Posted February 2, 2012 Oblig http://xkcd.com/221/ 1 Link to comment Share on other sites More sharing options...
John Cuthber Posted February 2, 2012 Share Posted February 2, 2012 Optional Dilbert cartoon. http://dilbert.com/strips/comic/2001-10-25/ Link to comment Share on other sites More sharing options...
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