delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1999/09/25/03:36:18

From: mrh99 AT uswest DOT net (Matthew Haley)
Newsgroups: comp.os.msdos.djgpp
Subject: Re: srand()
Organization: Home
Message-ID: <37ec69fb.17055060@news.uswest.net>
References: <7shlcr$bn2$1 AT canopus DOT cc DOT umanitoba DOT ca>
X-Newsreader: Forte Free Agent 1.11/32.235
Lines: 46
Date: Sat, 25 Sep 1999 06:49:17 GMT
NNTP-Posting-Host: 209.181.102.70
X-Trace: news.uswest.net 938241996 209.181.102.70 (Sat, 25 Sep 1999 01:46:36 CDT)
NNTP-Posting-Date: Sat, 25 Sep 1999 01:46:36 CDT
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

Hi Randy , 

I learn best by example so here's a simple C program:

/*** srand_demo.c ***/
#include <stdio.h>	/* printf(), NULL 			*/
#include <stdlib.h>	/* srand(), EXIT_SUCCESS 	*/
#include <time.h>	/* time() 					*/

#define RANDOM_RANGE	32767

int main(void)
{
	int iRandomNumber;

	srand(time(NULL));
	iRandomNumber = rand() % RANDOM_RANGE;

	printf("Here's a random number between 0 and %i -> %i\n",
			RANDOM_RANGE, iRandomNumber);

	return (EXIT_SUCCESS);
}

/*** end of srand_demo.c */

As an aside, I built this program with lcc-Win32 (I don't have DJGPP
installed right now :), then ran it 645 times and it actually produced
numbers between ~28,000 and ~30,000 so it's not the best way to get
random numbers, unless you modify the result with a bit more math.

 On Fri, 24 Sep 1999 23:57:58 -0400,
 <<Randy>> wrote:

>To use rand() one should call srand() with a seed...
>Is there a really simple way to get the time?  Would I have to include
>time.h?
>
>
>

===================================
Matthew Haley
mhaley99 AT homemail DOT com
http://www.users.uswest.net/~mrh99
===================================

- Raw text -


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