Jump to content
Tuts 4 You

how to generate a random number


DavidOliveira

Recommended Posts

DavidOliveira

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 everytime

how can i fix it

Link to comment

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 by Kripton
Link to comment
DavidOliveira
Use time functions - like 'GetTickCount' - and mix results to get random numbers .

how i dont know this function could give a example

Link to comment

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 example

using GetTickCount requires knowledge in win32 API programming. try google one of those.

Edited by Lumiliyab
Link to comment
GamingMasteR
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 .

Link to comment
DavidOliveira
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 example

using GetTickCount requires knowledge in win32 API programming. try google one of those.

thanks man i fix it!!!

Link to comment

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.

Link to comment

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...