delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/09/22/22:15:28

From: horst DOT kraemer AT snafu DOT de (Horst Kraemer)
Newsgroups: comp.os.msdos.djgpp
Subject: Re: problem with rand()
Date: Wed, 23 Sep 1998 02:05:15 GMT
Organization: [Posted via] Interactive Networx
Lines: 34
Message-ID: <36083ca8.113940@news.snafu.de>
References: <000301bde661$b7465d20$fd8f2499 AT Internet DOT usinet DOT pminerv>
NNTP-Posting-Host: n246-83.berlin.snafu.de
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

On Tue, 22 Sep 1998 14:46:20 -0500, "Paul Minerva" <p_minerva AT msn DOT com>
wrote:

>I am writing an application that needs the rand() function.  When I compile
>this program using the UNIX g++ it works fine.  When I compile the program
>using gcc, it does not seem to work correctly.
>Any idea what the problem is?

What does it mean that your snippet

      #include <stdio.h>
      #include <stdlib.h>
      main() {
	int r = rand();
	printf("%d\n",r);
 	return r;
     }

does not seem to work correctly ? Are you complaining that it will
always return 0 ? This is perfectly correct. Every rand() will always
return the same number after the first call to rand() in a program.
Sometimes it is 123456782, sometimes it is 0.

If you want "unpredictable" random numbers you may call

        #include <time.h>  
	srand(time(0));

_once_ in your program before calling rand() for the first time. 


Regards
Horst

- Raw text -


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