delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/02/01/20:45:28

From: "John M. Aldrich" <fighteer AT cs DOT com>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: random number in range
Date: Sun, 01 Feb 1998 19:03:45 -0500
Organization: Two pounds of chaos and a pinch of salt.
Lines: 21
Message-ID: <34D50D61.F9D@cs.com>
References: <34D50550 DOT 48ED AT quantum DOT de>
NNTP-Posting-Host: ppp203.cs.com
Mime-Version: 1.0
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

Tom Chojnacki wrote:
> 
> Hi!
> 
> A little problem:
> how can I get random number from a given range.

x = random() % 100   /* x will be 0 to 99 */

The mod operator (%) returns the remainder from the division of its
operands.  As long as the random number generator you're using is
suitably random in its low bits (which DJGPP's random() is), using mod
is an excellent way to clip to the desired range.

-- 
---------------------------------------------------------------------
|      John M. Aldrich       | "Animals can be driven crazy by pla- |
|       aka Fighteer I       | cing too many in too small a pen.    |
|   mailto:fighteer AT cs DOT com   | Homo sapiens is the only animal that |
| http://www.cs.com/fighteer | voluntarily does this to himself."   |
---------------------------------------------------------------------

- Raw text -


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