www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1996/08/29/11:28:08

From: kagel AT quasar DOT bloomberg DOT com
Date: Thu, 29 Aug 1996 11:14:17 -0400
Message-Id: <9608291514.AA04578@quasar.bloomberg.com >
To: macleod AT onyx DOT interactive DOT net
Cc: djgpp AT delorie DOT com
In-Reply-To: <DwvF3A.H2r@interactive.net> (macleod@onyx.interactive.net)
Subject: Re: random() and rand() question
Reply-To: kagel AT dg1 DOT bloomberg DOT com

   Errors-To: postmaster AT ns1
   Xref: news2.mv.net comp.os.msdos.djgpp:7990
   Newsgroups: comp.os.msdos.djgpp
   From: macleod AT onyx DOT interactive DOT net (J.J. Pierson )
   X-Newsreader: TIN [UNIX 1.3 unoff BETA release 960817]
   X-Nntp-Posting-User: macleod
   Sender: news AT interactive DOT net (System Administrator)
   Organization: IBS Interactive, Inc.
   Lines: 10
   X-Trace: 841273893/19779
   X-Nntp-Posting-Host: onyx.interactive.net
   Date: Wed, 28 Aug 1996 23:11:34 GMT
   Dj-Gateway: from newsgroup comp.os.msdos.djgpp
   Content-Type: text
   Content-Length: 576

   Silly question... How would I go about getting a random number from say 1
   to a specified number?

just call random(), after using srandom() to seed the generator, modulo the
result and add one.  The following will give a number between 1 and 50 
inclusive:

long num;

#define MAX_RAND 50

srandom( time(NULL) );

num = random();
num = (num % MAX_RAND) + 1;

-- 
Art S. Kagel, kagel AT quasar DOT bloomberg DOT com

A proverb is no proverb to you 'till life has illustrated it.  -- John Keats

- Raw text -


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