Jump to content
  • Advertisement
Sign in to follow this  
zerotheend2000

random number again

This topic is 4830 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

hi there i want to create a random number that is different of everytime you run the exe of the program, my random number code that is in a random number gen class is here :
#include ".\randomnumber.h"

CRandomNumber::CRandomNumber(void)
{	
	srand(time(NULL));
}

CRandomNumber::~CRandomNumber(void)
{
}
/////////////////////////////////////////////////////////////////////////
// Generate a random number
/////////////////////////////////////////////////////////////////////////
int CRandomNumber::GenerateNumber()
{

int rnd = rand()%10+1;
return rnd;

}


What happens is that a number is generated but if i close the program down and run the program again the same numbers appear. How do i stop this and make it totally random?

Share this post


Link to post
Share on other sites
Advertisement
That should be fine providing the constructor is getting called, the member not being statically called also.

ace

Share this post


Link to post
Share on other sites
well yes it works but, the numbers are only random on 1 execute of the program . . after that you can predict the outcome . . ur answer is a thought tho, the function is working like the srand is having no effect and working just like rand() . . hmmm ill have a look

edit::

yes ineed that was the problem, i had a pointer to my randclass in the class i call this from and for some reason it wasnt calling the constructor this way . . hmm, so i added an instane of the class as a global for now

thanks

Share this post


Link to post
Share on other sites
Just to make sure that time() is returning something useful you could bring that out of the srand() call and check its value.

ace

Share this post


Link to post
Share on other sites
One problem that you're going to run up against is that if you call the exe quickly, time(NULL) may return the same value, and therefore rand() may return the same value.

You could try /dev/urandom (unix) to seed srand, or on its own just to return a number. You would need something like MinGW I think to access /dev/urandom under Windows.

Share this post


Link to post
Share on other sites
I wouldn't have thought that this would be the problem. Although it might be worth using timeGetTime() to seed the srand().

ace

Share this post


Link to post
Share on other sites
Quote:

Anyone who considers arithmetical methods of producing random digits is, of course, in a state of sin.
- John von Neumann (1951)


Given a number you can always predict the next (or choose a small subset of most probable numbers).
Given the seed you know the entire sequence!
In practice you have only deterministic sequences that you call pseudo-random. The fact that in most implementation RAND_MAX is only 32768 does not help you if you want a fine generator.

In practice:

If you want a fast and good algorithm to produce uniform random numbers try
the Marsenne Twister algorithm (google for it and you will find every implementation from java to php, from C to C++).
It's faster than rand and, most important, its cycle is so long you never reach the end.
To set the seed use the timer or something (pseudo)random number. [smile]

Share this post


Link to post
Share on other sites
Quote:
Original post by blizzard999
The fact that in most implementation RAND_MAX is only 32768 (...)


Humm? I thought we had at least 32-bit processors and OS's these days... oh well :) Maybe some ages-old compilers might have a 16-bit int, but most? Doubtful.

For the original poster: Are you 100% sure that you remembered to actually assign a new'd object to the pointer? That is,

Random *random;
...
random = new Random;
// or in constructor initializer list: random(new Random)


If you didn't, the pointer's contents are garbage, and calling a member function through it yields undefined behaviour - but, in this case, as the function doesn't access the object at all, it probably "works" (that is, compiles but doesn't crash), but doesn't do what is expected, because no one ever called the constructor.

Share this post


Link to post
Share on other sites
Quote:

Although it might be worth using timeGetTime() to seed the srand().


I have found this method to work better in the past.


srand( timeGetTime(NULL) );




[Edited by - guyaton on August 30, 2005 10:34:08 AM]

Share this post


Link to post
Share on other sites
Quote:
Original post by Sharlin
Quote:
Original post by blizzard999
The fact that in most implementation RAND_MAX is only 32768 (...)


Humm? I thought we had at least 32-bit processors and OS's these days... oh well :) Maybe some ages-old compilers might have a 16-bit int, but most? Doubtful.

The size of int has nothing to do with the value of RAND_MAX (except that the minimum allowable value for RAND_MAX is the same as the minimum allowable value for INT_MAX). Here are the values of RAND_MAX on some modern compilers:

Borland 5.6.4:
#define RAND_MAX 0x7FFFU

gcc 3.3.1:
#define RAND_MAX 0x7FFF

MSVC++ 7.1:
#define RAND_MAX 0x7fff


Enigma

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • 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!