random number again

Started by
11 comments, last by blizzard999 18 years, 7 months ago
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?
Advertisement
That should be fine providing the constructor is getting called, the member not being statically called also.

ace
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
Just to make sure that time() is returning something useful you could bring that out of the srand() call and check its value.

ace
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.
--random_thinkerAs Albert Einstein said: 'Imagination is more important than knowledge'. Of course, he also said: 'If I had only known, I would have been a locksmith'.
I wouldn't have thought that this would be the problem. Although it might be worth using timeGetTime() to seed the srand().

ace
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]
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.
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]
~guyaton
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

This topic is closed to new replies.

Advertisement