Archived

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

Le0

rand() help!

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
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
It''s more like learning Dutch while standing in the middle of Berlin. (FYI: Dutch and German are related languages with many similarites, but also many differences). OpenGL is an API based firmly in C. Usable in C++, but still quite unlike the actual object oriented C++ stuff most books will teach you.

Share this post


Link to post
Share on other sites
As a piece of advice, you should know that all PRNG algorithms that I am aware of will eventually cycle into a repeated pattern. If you are generating a very large number of psuedorandom numbers (ten thousand or more), it may be wise to periodcally reseed the RNG. You can wrap the whole functionality into a single function like this:

      
#include <stdlib.h> /* in C++, use <cstdlib> instead */

#include <time.h> /* in C++, use <ctime> instead */

#define INTERVAL 1024 /* modify this as needed to optimize results */


double random()
{
static int count = 0;

if (0 == count)
srand(time(0));

count++;
count %= INTERVAL; /*if count==INTERVAL, then set count to 0*/

return rand();
}


You should set the INTERVAL constant to one that seem to get the best results under your system.

You should also know that many (but not all) newer PC motherboards have hardware random number generators. Accessing them, however, depends on the motherboard and the OS you are using. In Linux, IIUC, this functionality is accessed with the /dev/rand pseudodevice (though not all RNGs are supported); I don't know offhand how it would be accessed in Windows.

Trouble rather the tiger in his lair than the Scholar among his books

[edited by - Schol-R-LEA on November 6, 2002 4:41:08 PM]

Share this post


Link to post
Share on other sites