Jump to content
  • Advertisement

Archived

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

garconbifteck

Random Number

This topic is 6021 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
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;
}


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
Guest Anonymous Poster
tried msdn?

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!