cplusplus.com cplusplus.com
cplusplus.com   C++ : Forums : Beginners : I need a good seed for an RNG
  Search:
- -
C++
Information
Documentation
Reference
Articles
Sourcecode
Forums
Forums
Beginners
Windows Programming
UNIX/Linux Programming
General C++ Programming
Articles
Lounge
Jobs

-

question  I need a good seed for an RNG

Nate879 (22)
How do I get a good seed for a random number generator? I think I need a function similar to 'clock' in 'time.h', but it should return the number of clock ticks since the computer started, so it doesn't return the same number every time.
|
psault (82)
add:
#include <time.h>

add:
srand((unsigned)time(NULL));
(before you call rand();)
|
Nate879 (22)
That only generates a different seed every second. I need a function that changes the seed faster than that.
|
EWu (12)
I think the correct code is:
1
2
3
4
5
6
srand(unsigned(time(NULL)));
//then you should use an array where you assign the random values
long A[20];
for(int i=0;i<20;i++)
{A[i]=rand();
}

This should assign the vallues more faster.
|
Nate879 (22)
I mean the seed, not the random numbers generated from it.
|
psault (82)
Is there a way you could manipulate your random results using a loop and math expressions for each second that the clock is seeding your generator?
That's the only thing I can think of trying, since I don't know how else to seed the generator.
|
psault (82)
Is there a way you could manipulate your random results using a loop and math expressions for each second that the clock is seeding your generator?
That's the only thing I can think of trying, since I don't know how else to seed the generator.
|
cyberpirate (26)
try
1
2
3
4
5
6
7
int seed;
for(seed=1;;seed++);
{
srand(seed);
seed = rand();
printf("%d\n",seed);
}


hope it helps
|
AzraelUK (57)
This is far better: http://xkcd.com/221/
|

This topic is archived - New replies not allowed.
Home page | Privacy policy
© cplusplus.com, 2000-2008 - All rights reserved - v2.2
Spotted an error? contact us