Advertisement

Random Number

Started by March 23, 2002 05:29 PM
14 comments, last by garconbifteck 22 years, 8 months ago
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.
In C:

  #include <time.h>#include <stdio.h>#include <stdlib.h>int main(void) {  int RandVal;  srand(time(NULL));  RandVal = rand();  printf("A random number: %d\n",RandVal);  return 0;}  


Advertisement
Oops sorry, I forgot to put in C++
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]
Thanks PO
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...
Advertisement
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
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().

  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?
tried msdn?

This topic is closed to new replies.

Advertisement