Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Le0

rand() help!

This topic is 5852 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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;
}

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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?

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!