delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2007/09/04/15:15:05

X-Authentication-Warning: delorie.com: mail set sender to djgpp-bounces using -f
From: "'" <eilatgreen AT hotmail DOT com>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: random() : What am I doing wrong?
Date: Tue, 04 Sep 2007 12:08:12 -0700
Organization: http://groups.google.com
Lines: 55
Message-ID: <1188932892.279559.213990@g4g2000hsf.googlegroups.com>
References: <N1GBi.41236$S91 DOT 9560 AT newsfe7-win DOT ntli DOT net>
NNTP-Posting-Host: 88.153.183.115
Mime-Version: 1.0
X-Trace: posting.google.com 1188932893 23138 127.0.0.1 (4 Sep 2007 19:08:13 GMT)
X-Complaints-To: groups-abuse AT google DOT com
NNTP-Posting-Date: Tue, 4 Sep 2007 19:08:13 +0000 (UTC)
In-Reply-To: <N1GBi.41236$S91.9560@newsfe7-win.ntli.net>
User-Agent: G2/1.0
X-HTTP-UserAgent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1),gzip(gfe),gzip(gfe)
Complaints-To: groups-abuse AT google DOT com
Injection-Info: g4g2000hsf.googlegroups.com; posting-host=88.153.183.115;
posting-account=ps2QrAMAAAA6_jCuRt2JEIpn5Otqf_w0
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com
Errors-To: nobody AT delorie DOT com
X-Mailing-List: djgpp AT delorie DOT com
X-Unsubscribes-To: listserv AT delorie DOT com

On Aug 30, 11:47 pm, "MikeC" <My_addr DOT  DOT  DOT  AT end DOT of DOT post> wrote:
> Folks,
>
> Here's an open invitation to make me feel foolish!
>
> Here's the program :
>
> #include <time.h>
> #include <stdlib.h>
>
> main()
> { long lng;
>
>   lng = rawclock();
>   printf("\nClock = %ld - ", lng);
>   srand(lng);
>   printf("Random numbers = %ld;  %ld;  %ld",random(),random(),random());
>
> }
>
> The idea is to seed the random number generator from the clock, so that the
> first call to random() will always produce a different result.
>
> When I run the program several times, here's what it yields :
>
> Clock = 1392294 - Random numbers = 1027100827;  143302914;  2078917053
> Clock = 1393105 - Random numbers = 1027100827;  143302914;  2078917053
> Clock = 1393314 - Random numbers = 1027100827;  143302914;  2078917053
> Clock = 1393420 - Random numbers = 1027100827;  143302914;  2078917053
> Clock = 1393471 - Random numbers = 1027100827;  143302914;  2078917053
> Clock = 1393512 - Random numbers = 1027100827;  143302914;  2078917053
> Clock = 1393548 - Random numbers = 1027100827;  143302914;  2078917053
>
> The 'random' numbers are the same every time I run the program, yet they are
> being seeded be a clock value that is different each time.  I guess the wood
> is there somewhere, but the trees are getting in the way.
>
> Incidentally, I'm running Windows 2000, SP4.
>
> Advance thanks,
>
> MikeC.
> --
> Mental decryption required to bamboozle spam robots:
>
> mike_best$ntlworld*com
> $ = @
> * = dot

Hi
The problem is that srand() work on rand() and not on random().
You have to take srandom() instead srand() and the program will work
fine.
Moshe

- Raw text -


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