Sign in to follow this  
zerotheend2000

random number again

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
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
Darn, you are correct. In my libc (gcc 3.4), RAND_MAX was 2^31-1, and I didn't see any reason why it would be less than INT_MAX in any library - or at least, why it would be as small as 32767 with a 4-byte int.

Share this post


Link to post
Share on other sites
Quote:
Original post by Sharlin

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.

Darn, you are correct. In my libc (gcc 3.4), RAND_MAX was 2^31-1, and I didn't see any reason why it would be less than INT_MAX in any library - or at least, why it would be as small as 32767 with a 4-byte int.


I'm wrong that RAND_MAX is usually 32768...in fact it is 32767 that is 2^15 - 1.
MSVC 6 is 32767, I already tested some time ago .
To be 100% sure I wrote a complex program; I show you the code


#include <iostream>
#include <math.h>

int main(){
std::cout << RAND_MAX << std::endl;
return 0;
}



It reports 32767. This time compiled under XP with CodeBlocks/Mingw/gcc (latest version).

In stdlib.h you can read


/*
* RAND_MAX is the maximum value that may be returned by rand.
* The minimum is zero.
*/

#define RAND_MAX 0x7FFF



Why RAND_MAX is only 32767 ?

Because it sucks. [smile]

Dont use rand() if you need random numbers. The alternatives are simply and efficient.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this