delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/01/22/23:15:33

From: "John M. Aldrich" <fighteer AT cs DOT com>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: random numbers?
Date: Thu, 22 Jan 1998 23:08:12 -0500
Organization: Two pounds of chaos and a pinch of salt.
Lines: 51
Message-ID: <34C817AC.38C9@cs.com>
References: <Pine DOT SUN DOT 3 DOT 91 DOT 980122183421 DOT 1427F-100000 AT is> <34C78C11 DOT 3D2898C6 AT fuw DOT edu DOT pl> <6a82ee$k6k$1 AT news DOT ox DOT ac DOT uk> <34C797D7 DOT 4FE23B3F AT fuw DOT edu DOT pl>
NNTP-Posting-Host: ppp244.cs.com
Mime-Version: 1.0
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

Mirek Prywata wrote:
> 
> George Foot wrote:
> >
> > : That's true, I was wrong. I simply meant
> > : int random(int) and not int random (void)
> >
> > ... but even so, random does not take an integer.  I don't have djgpp
> > here to check, but the prototype should be `int random (void);'.
> 
> if you want to have random numbers, but not as big as RAND_MAX (or
> MAX_RAND - I don't remember) then you can easily make random() that
> returns numbers from <0,MY_RAND_MAX>. That's random(int) i meant.

Wrong again.  Unless you are using C++, you can't name your own function
the same as a library function that it calls.  The following code is
logically and syntactically incorrect:

#include <stdlib.h>
int random( int x )
{
    return random() % x;
}

That code will not compile, unless you are using a truly dumb compiler,
or you're using C++'s overload features, in which case this discussion
properly applies to C++, not C.  Anyway, overloading a library function
not designed specifically for C++ is a Bad Thing, IMHO.

If your only concern is that Borland's random() takes an integer range,
and DJGPP's doesn't, then you can conditionally compile your code to use
random(x) on Borland, and random() % x on DJGPP.

> IMHO there's no point in using different generator from rand(). If you
> want to have better generator you must write it on your own, in order to
> know, how it works.

The DJGPP random() function is non-ANSI, but it uses a much improved
algorithm over the ANSI-standard rand() library function.  If you write
code mainly for DJGPP, there's no reason not to use random().  If you
write code that must be portable, then it's best either to use #ifdef's
to conditionally use whatever RNG features exist for a given compiler,
or to write your own RNG from scratch (because their quality varies
highly from compiler to compiler).

-- 
---------------------------------------------------------------------
| John M. Aldrich, aka Fighteer I  |     mailto:fighteer AT cs DOT com     |
| Plan:  To find ANYONE willing to |   http://www.cs.com/fighteer   |
| play Descent 2 on DWANGO!        | Tagline: <this space for rent> |
---------------------------------------------------------------------

- Raw text -


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