delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1999/11/02/18:53:10

Message-ID: <381F6A57.A34933A0@lycosmail.com>
Date: Tue, 02 Nov 1999 17:48:55 -0500
From: Adam Schrotenboer <ajschrotenboer AT lycosmail DOT com>
X-Mailer: Mozilla 4.7 [en] (Win98; U)
X-Accept-Language: en
MIME-Version: 1.0
To: djgpp AT delorie DOT com
Subject: Re: rand and random
References: <s1n73iu5iqi71 AT corp DOT supernews DOT com>
Reply-To: djgpp AT delorie DOT com

If you're asking what I think you're asking, this might help you.

First, I should tell that there is an easy method for getting (integers
only {in the mathematical sense, not the 16-bit vs 32-bit vs 64-bit
sense}).

It is something like this

srand(time(0));  //Initializes random number generator
rand() % 100 + 1; // ANSI & POSIX, portable
// or
srandom(time(0));  //Initializes random number generator
random() % 100 + 1; // Non ANSI, Non POSIX, non portable, better
randomness of lower bits.

Which returns a random number 1 to 100. (in this example the return
value goes nowhere, merely goes to the next number in the sequence.)

The above method works for many instances. However, there is a better
method that bypasess some of the problems with the randomness of the
lower bits (a known problem). I'm going to use the ANSI & POSIX
compatible functions for this example.

float rand_float(float low, float high)
{
    return (float)(rand()/(float)RAND_MAX)*(high-low)+low);
}

long rand_int(long low, long high)
{
    return (long)((rand()/(float)RAND_MAX)*(high-low+1)+low);
}

Neither of these is optimized for speed, and may be slow. I don't know
for sure, but they work well if you need to be able to have numbers
within a range, and they appear to work for signed as well as unsigned.
Just be careful in your typecasting. Another suggestion is to consider
declaring them inline, or maybe, if you know all the intricacies of ti,
try making them a #define macro.

in the rand_int, you may have noticed that as opposed to in rand_float,
there is (high-low+1) vs (high-low). It may appear weird, but that's the
only way I've found to make it work. Various typecasts are to make the
math be done right (like in ...(float)RAND_MAX..., where you probably
want float division, rather than integer division), and to stop the
compiler from giving compiler warnings (like in (return
(long)(rand()...).

One last note, if you need long long (64-bit integers) or double, or
long double (not altogether recommended, as it is only supported [AFAIK]
on x86), make sure to make the divisions done w/ a precsion meeting or
exceeding the output, like this

long long rand_int(long low, long high)
{
    return (long long)((rand()/(double)RAND_MAX)*(high-low+1)+low);
}

That way you are doing a 64-bit precision division, which (Though
there's probably somebody out there who can correct me) will give you
sufficient precision for a long long.

Anyway, good luck.

Al Morgan wrote:

> How does one use the rand(), and random() functions?
>
> Thanks,
>     Al [ muaddib AT proaxis DOT com ]

- Raw text -


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