rand() help!

Started by
10 comments, last by Le0 21 years, 5 months ago
Im having some issues with the rand function, i use it sometimes and i think that the result from it are very far from random, i get a lot of same number. Anyone got any idea whats going? Leo
"The bible is probably the most genocidal book ever written!" Noam Chomsky
Advertisement
Rand is a pseudo random number generator. It generates random numbers but it will generate the same numbers everytime you start it.

If you put: srand( (unsigned)time( NULL ) );
in your code the random number generator will be seeded with the current time so you get different values everytime you start it
try using this in your code.

#include <iostream>
#include <time.h>
#include <stdlib>
main()
{
int test;
bool go;

go = true;

if (go = true){
srand(time(NULL));
test = rand()%10;
std::cout << test;
}

return 0;
}
very basic C++ example, but should give you random numbers. I'm pretty sure this is the code, but I will have to check at home, for I am at office.


[edited by - ThomasSauder on November 4, 2002 3:16:55 PM]
rand() will give you the same numbers every time you execute your program. use srand( time( 0 ) ); instead, that way you get different results because the randomizer is seeded by the internal clock of the computer. or you can seed it manually by doing this:

// necessary includes
int main()
{
unsigned seed;
cout << "Enter seed: ";
cin >> seed;
srand( seed );
// rest of program and error checking.

return 0;
}

P.S dont forget to use #include <ctime> and #include <cstdlib> or you''ll get a syntax error.
Call srand only once at the beginning of the program, then call rand where you want the random number.
Arguing on the internet is like running in the Special Olympics: Even if you win, you're still retarded.[How To Ask Questions|STL Programmer's Guide|Bjarne FAQ|C++ FAQ Lite|C++ Reference|MSDN]
Can''t you also include windows.h and before randomizing the number, use srand(GetTickCount())?

For example:

#include <iostream.h>
#include <windows.h>

int main()
{
int number = 0;
srand(GetTickCount());
number = rand()%(rangevalue);
cout << number;
return 0;
}
Yes, but then you can only use the code in Windows.
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other player's eyes with his King.
thanks a lot guys, i will investigate that probleme further tomorow, because my tetris is going well, but i still think that my random is bugged, and i seeded it will see later

"The bible is probably the most genocidal book ever written!" Noam Chomsky
"The bible is probably the most genocidal book ever written!" Noam Chomsky
Question about this whole random number thing in C++.

I am coding a Battletech game and I need to call a function that simulates rolling 2 6-sided die. It sounds like I will be getting the same number each time I call the function. Is that true? I will only get a new number when I restart the program?
I know is BASIC they had a random number function that was seeded by the clock. Same deal here?

How does that work?
In c++ you also seed the random number generator with the time looks like

(other includes)
#include <ctime>

srand(time(0));

to get the number call the
rand()
function

Hmm learning OpenGL and C++ at the same time is like trying to learn french while in the middle of Paris
Hmm learning OpenGL and C++ at the same time is like trying to learn french while in the middle of Paris

This topic is closed to new replies.

Advertisement