delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/06/11/01:46:58

From: George Foot <mert0407 AT sable DOT ox DOT ac DOT uk>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Random function gives me the same numbers!
Date: 11 Jun 1998 04:14:12 GMT
Organization: Oxford University, England
Lines: 33
Message-ID: <6lnlik$962$2@news.ox.ac.uk>
References: <357F3A1C DOT E19378BD AT netrover DOT com>
NNTP-Posting-Host: sable.ox.ac.uk
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

On Wed, 10 Jun 1998 21:59:57 -0400 in comp.os.msdos.djgpp Nicolas
Blais <eletech AT netrover DOT com> wrote:

: Hi, the function below seams to always give me the same set of numbers
: in R_C everytime I run it, can it be fixed?

: srandom(clock()/CLOCKS_PER_SEC);
: int R_C = random() / (RAND_MAX / 255 + 1);

Yes.  If you look at the documentation for `clock' you'll see that it
returns the time since you first called it; i.e. the first time you
call it it will return `0'.  So you're not seeding with different
numbers.

A better function to use to get a different number each run is the
`time' function.  You can use it like this:

    #include <time.h>
    #include <stdlib.h>
    ...
    srandom (time (NULL));

`time' counts up in seconds in djgpp, so provided your runs are
separated by more than one second you'll get a different sequence each
time.

If that's not good enough you still have a few more options, but
generally `time' is sufficient.

-- 
george DOT foot AT merton DOT oxford DOT ac DOT uk

xu do tavla fo la lojban  --  http://xiron.pc.helsinki.fi/lojban/lojban.html

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019