Archived

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

garconbifteck

Random Number

Recommended Posts

I have done a search on these forums and found answers, but none of them worked I am making a little text-rpg and I need to be able to get a random number so I can do if/else to see what is going to happen when they choose to visit a certain area etc. I did this unsigned short int random = rand(); But that was returning 41 everytime, then I realised that it gives the same number everytime it''s ran from a post in another thread. In the other thread the person went on to explain srand() but didn''t give a proper example, could someone please give me an example of a variable that has a value that is different everytime the program is ran so that I can take a look at it and try to understand it that way. I searched through my book and it never explained it anywhere.

Share this post


Link to post
Share on other sites
here is an example of srand(), in C++


      
#include <time.h>

#include <stdlib.h>

#include <iostream>

using std::cout;
using std::endl;

void main( void )
{
// Seed the random number on the current time

srand( ( unsigned ) time( NULL ) );

// Display the random number using the regular rand() funtion

cout << rand() << endl;

// Different every time

cout << rand() << endl;

}


That's it!

EDIT: EEK!! Syntax
_____________________________________________________

ICQ #: 149510932

Google - OpenGL - DirectX - Windows Guide Network - MSDN - Symantec Virus Info

"Imagination is more important than knowledge." - Albert Einstein



[edited by - Programmer One on March 23, 2002 6:49:56 PM]

[edited by - Programmer One on March 23, 2002 6:52:07 PM]

Share this post


Link to post
Share on other sites
random numbers are based on seeds. when you start ur program, the seed is always the same, so you will get the same results. this is good for experiments -being able to duplicate them exactly. if you dont care for that, you can reseed the number generator using something like time in ms. if you seed the generator to a time, then it will run the same at that time of day, which is why you want to use miliseconds, and not hours or months...

Share this post


Link to post
Share on other sites
quote:
Original post by EvilCrap
random numbers are based on seeds. when you start ur program, the seed is always the same, so you will get the same results. this is good for experiments -being able to duplicate them exactly. if you dont care for that, you can reseed the number generator using something like time in ms. if you seed the generator to a time, then it will run the same at that time of day, which is why you want to use miliseconds, and not hours or months...



I''ve actually noticed that, thanks. How do I get it into milliseconds then?, i''ll go search on google now, but if anyone can help ... ^_^

"Computer games don''t affect kids. I mean if Pac-Man affected us as kids, we''d all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." -Kristian Wilson, Nintendo, Inc, 1989

Share this post


Link to post
Share on other sites
As a side note, do not initialise global variables with rand() like this :

int globalRandomVar() = rand();

Because this rand() will be called before the main() function (and therefore before you initialise the seed) and will always return the same value.

It doesn''t seem that''s what you''re doing but anyway knowing this may save you much time some day.

> How do I get it into milliseconds then ?

Under Windows, call either GetTickCount() or timeGetTime().

Share this post


Link to post
Share on other sites

  
int randomnum()
{
srand( (unsigned short int) time(NULL) );
return(rand());
}


Well i''ve finished the game, except this, how do I make this use GetTickCount()?, I tried changing time(NULL) to GetTickCount(NULL) but that never worked.

anyone?

Share this post


Link to post
Share on other sites
quote:
Original post by EvilCrap
#include <windows.h>

srand(GetTickCount());



thanks!, my first game done ... even if it is text only...



"Computer games don''t affect kids. I mean if Pac-Man affected us as kids, we''d all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." -Kristian Wilson, Nintendo Inc. 1989

Share this post


Link to post
Share on other sites
garconbifteck: You should *not* reseed the random-number generator using srand every time you generate a new number.
For most applications it suffices to call srand once when the program starts up. For servers that runs for days or weeks you should probably call srand once every now and then (say once a day or so).

Neophyte

- Death awaits you all with nasty, big, pointy teeth. -

Share this post


Link to post
Share on other sites
quote:
Original post by Neophyte
garconbifteck: You should *not* reseed the random-number generator using srand every time you generate a new number.
For most applications it suffices to call srand once when the program starts up. For servers that runs for days or weeks you should probably call srand once every now and then (say once a day or so).

Neophyte

- Death awaits you all with nasty, big, pointy teeth. -



What will it do? It seems to work fine and it''s hardly going to be munching memory on a text game, right?


"Computer games don''t affect kids. I mean if Pac-Man affected us as kids, we''d all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." -Kristian Wilson, Nintendo Inc. 1989

Share this post


Link to post
Share on other sites
quote:
Original post by garconbifteck
What will it do? It seems to work fine and it''s hardly going to be munching memory on a text game, right?

Well, here''s the secret. rand isn''t random at all. It''s technically psuedorandom, in that the sequence of numbers appears random to the untrained eye, but it''s actually very reproducible. srand sets a "seed" value from which the sequence of numbers is generated, and it''s the same every time for the same given seed. So if you constantly re-seed the generator, then you increase the probability of passing it the same number more than once which makes your app appear less random.

Which isn''t what we think you want to do.

[ GDNet Start Here | GDNet Search Tool | GDNet FAQ | MS RTFM [MSDN] | SGI STL Docs | Google! | Asking Smart Questions ]
Thanks to Kylotan for the idea!

Share this post


Link to post
Share on other sites
Sure it would increase the chance of it repeating, but it would probably be a very slight increase. It''s not like the same number is going to come up three times in a row. That would be like a one in a million chance... (maybe less than a million)

Massif - "It means mountain."

Share this post


Link to post
Share on other sites