www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/02/25/22:15:53

From: "John M. Aldrich" <fighteer AT cs DOT com>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: How to limit a random() with DJGPP ?
Date: Wed, 25 Feb 1998 22:07:37 -0500
Organization: Two pounds of chaos and a pinch of salt.
Lines: 27
Message-ID: <34F4DC79.6499@cs.com>
References: <01bd41f9$d7ef7aa0$cd2f86c2 AT pieter>
NNTP-Posting-Host: ppp204.cs.com
Mime-Version: 1.0
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

Pieter van Ginkel wrote:
> 
> #define Random(x) (random() % x)
> and then just use Random. This works quite well. The only thing with this is
> that you can't get random numbers bigger that MAXINT. I think rand() does
> this but you should find that out yourself.

What's wrong with MAXINT?  It is 2^32 in a 32-bit program.  If you're
really nuts about high-power integer numbers, write your own RNG that
uses a long long seed instead of int.

> If you want to have a random function that gives a floatingpoint result,
> usae something like this.
> 
> #define FPRandom() (random() / (double)MAXINT)

This is not a really good solution because its granularity will still be
limited to the range of MAXINT.  For really good random floating point
values, a custom RNG is almost mandatory.

-- 
---------------------------------------------------------------------
|      John M. Aldrich       | "Sex should be friendly.  Otherwise  |
|       aka Fighteer I       | stick to mechanical toys; it's more  |
|   mailto:fighteer AT cs DOT com   | sanitary."                           |
| http://www.cs.com/fighteer |                - Lazarus Long        |
---------------------------------------------------------------------

- Raw text -


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