Random number help

The Partridge Family were neither partridges nor a family. Discuss.
User avatar
npissoawsome
Posts: 114
Joined: June 8th, 2012, 3:01 pm

Re: Random number help

Post by npissoawsome » June 28th, 2012, 9:01 pm

Asimov wrote:Hi all,

I have been reading to get a better random number you can set the seed to the clock time of your computer with something like

unsigned seed = time(0);
srand(seed);

and you have to include #include <time.h> to use it.

I am not 100% certain how it works, but it might help.

Asimov

PS I am not even sure yet why unsigned is used over int LOL, but there you go.
hmm....

Well, I've encountered this method many times before, it's pretty much just setting the random number seed to the amount of seconds since January 1970 or something

I'm pretty sure you can just do this

Code: Select all

#include <ctime>
#include <iostream>
#include <cstdlib>

int main()
{
     srand(time(NULL));
}
Also, you can't initialize a variable with just unsigned. Unisgned is like an extension to a type of integer

a signed short, or just short is an integer from -32768 to 32767
while an unsigned short is an integer from 0 to 65535

this is how you use them

Code: Select all

short sn; // -32768 - 32767
unsigned short usn; // 0 - 65535
...

just a quick note

Asimov,
when you include c libraries into a C++ application, most people do it like this

Code: Select all

#include <ctime>
instead of

Code: Select all

#include <time.h>
I think it looks a little nicer, and it shows you that the libraries you're including are C, not C++

Post Reply