Posted by Morgan Terry on 1999-10-04
an easy way to do it is to just put
srand(time(NULL));
at the top of your function. Remember to include time.h so you can use
the time() function.
example:
#include <iostream.h>
#include <stdlib.h>
#include <time.h> // for time() function
int main(void)
{
int num;
srand(time(NULL));
cout << "Here is a random number: " << rand();
return 0;
}
this will give you a more random sequence of numbers. It uses the
current time as a seed to the random number function.
Stormbringer wrote:
>
> Hi!
>
> If you get the system date, and use it to seed your rand() function, it will
> guarantee a different result every time.
>
> I did it in a project a while ago, but I don't remember what the code was
> I think it was getdate() or something, but don't quote me on that one) ,...
> but a very loose interpretation ( in "sort-of pseudo code" ) would be:
>
> function
> {
> get system date
> call rand or srand with variable storing system date as argument
> }
>
> int main
> {
> call function
> use rand or srand in your program.
> }
>
> Wow! How cryptic was that?? If you need a better explanation of that, please
> let me know,... I'll try to explain it a little better.... :)
>
> Good Luck
> Tammi
> Programmer Analyst
> stormbringr@xxxxxxxxx.xxx
>
> -----Original Message-----
> From: P oins <poins@xxxxxxxxxx-xxxx.xxx>
> To: stormbringr@xxxxxxxxx.xxx <stormbringr@xxxxxxxxx.xxx>
> Date: Saturday, October 02, 1999 10:11 PM
> Subject: using rand for cplus
>
> >I need help using the rand and srand functions for c++
> >
> >I'm trying to have a different sequence of numbers come out every time,
> this is what I have but it doesn't work
> >
> >int main()
> >{
> >
> >rnumber=rand();
> >
> >}
> >
> >now when I run this it'll give me the same sequence over and over what I'm
> trying to really ask is How to use the srand function so that i can get a
> different sequence of numbers each time
> >
> >
> >_____________________________________________________________
> >Add email to your web site - FAST, FREE, and CUSTOMIZED!
> >............. http://www.everyone.net ..............
> >
> >> >
>
> >
--
Morgan
morgan@xxxxxxxxxxx.xxx
Clever signature goes here
Previous post | Next post | Timeline | Home