DavidOliveira Posted July 7, 2009 Posted July 7, 2009 i wanna generate random numbers with no repeats, i have this code#include <cstdlib>#include <iostream>using namespace std;int main(){int randomNumber;for(int i = 0; i < 15; i++) //print random #s 15 times{randomNumber = (rand() % 25) + 1;cout << randomNumber << endl;getchar();}return 0;}but it generate the same sequence 17-18-10-1-20-25-4-9-13-15-6-21-7-3-12 everytimehow can i fix it
Kripton Posted July 7, 2009 Posted July 7, 2009 (edited) Try:#include <cstdlib> #include <ctime> #include <iostream>using namespace std;int main() { srand((unsigned)time(0)); int random_integer = rand(); cout << random_integer << endl; }In most coding languages the random number call will produce the same numbers everytime. We use the time as a seed value and generate the numbers from that Edited July 7, 2009 by Kripton
GamingMasteR Posted July 7, 2009 Posted July 7, 2009 Use time functions - like 'GetTickCount' - and mix results to get random numbers .
DavidOliveira Posted July 7, 2009 Author Posted July 7, 2009 Use time functions - like 'GetTickCount' - and mix results to get random numbers .how i dont know this function could give a example
Bleed Posted July 7, 2009 Posted July 7, 2009 (edited) check this out: http://www.cplusplus.com/reference/clibrary/cstdlib/rand/Use time functions - like 'GetTickCount' - and mix results to get random numbers .The GetTickCount function retrieves the number of milliseconds that have elapsed since Windows was started. It does not do random stuff.how i dont know this function could give a exampleusing GetTickCount requires knowledge in win32 API programming. try google one of those. Edited July 7, 2009 by Lumiliyab
GamingMasteR Posted July 8, 2009 Posted July 8, 2009 The GetTickCount function retrieves the number of milliseconds that have elapsed since Windows was started. It does not do random stuff.And this elapsed time is random by itself, you can get the same value from GetTickCount more than one time rarely .
DavidOliveira Posted July 8, 2009 Author Posted July 8, 2009 check this out: <a href="http://www.cplusplus.com/reference/clibrary/cstdlib/rand/" target="_blank">http://www.cplusplus.com/reference/clibrary/cstdlib/rand/</a>Use time functions - like 'GetTickCount' - and mix results to get random numbers .The GetTickCount function retrieves the number of milliseconds that have elapsed since Windows was started. It does not do random stuff.how i dont know this function could give a exampleusing GetTickCount requires knowledge in win32 API programming. try google one of those.thanks man i fix it!!!
atom0s Posted July 8, 2009 Posted July 8, 2009 Kripton answered your question already. Because you are using rand() you need to call srand() with a dynamic seed to allow rand() calls to actually be random.Using timestamps, such as a unix timestamp, for your seed with other math involved is probably your best bet to have a unique seed as well as timestamps do not repeat in that manner.
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