On Fri, Jun 25, 2010 at 10:03 AM, Pushparaj Shetty <[email protected]> wrote:
> Hi,
> I need to generate random number to assign weights to graph edges. Using
> rand() will give same random sequence on every run. Kindly tell how to
> generate distinct random for every run.
> In Borland C there is randomize()? whats is the equivalent here?
You can use srand(unsigned int seed) to choose the randomization seed,
if that's what you want. To get a unique random sequence every time
you start your application, this is what I'd suggest (this is what I
do, a dirty hack):
(I'm writing this on the spur of the moment, just take the idea and
code it yourself);
unsigned int seed;
fd=open("/dev/random", RD_ONLY);
read(fd, &seed, sizeof(seed));
close(fd);
srand(seed);
What this approach essentially does is take the seed from this file
called "/dev/random". From the name, I have always guessed that
everytime you read /dev/random, it generates random input.
What I hope is happening is that "/dev/random" gets affected by
"noisy" inputs like frequency of keyboard usage, mouse behaviour etc
(these things are not unheard of, may be they happen if you have the
hardware generators). This random sequence generator might also be
taking inputs like variation in CPU temperature or disk usage to
"truly" randomize itself.
On a side note, one resource you might want to dig into is random
number generation by gnu scientific library (gsl)[1].
Hope this helps,
Sharad
[1]
http://www.gnu.org/software/gsl/manual/html_node/Random-Number-Generation.html
--
l...@iitd - http://tinyurl.com/ycueutm