🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.unix.programmer
Article #3550

Re: Can erand48 really generate a 1.0?

#3550
From: Shing-Fat Fred Ma
Date: Fri, 07 Mar 2003 04:39
69 lines
3580 bytes
Oops.  Forgot to copy the newgroups...

-------- Original Message --------
Subject: Re: Can erand48 really generate a 1.0?
Date: Thu, 06 Mar 2003 23:36:51 -0500
From: Shing-Fat Fred Ma <fma@doe.carleton.ca>
To: Ronald Bruck <bruck@math.usc.edu>

Ronald Bruck wrote:

> > -------- Original Message --------
> > Subject: Can erand48 really generate a 1.0?
> > Date: Thu, 06 Mar 2003 20:34:43 -0500
> > From: Fred Ma <fma@doe.carleton.ca>
> > Organization: Bell Sympatico
> > Newsgroups: comp.unix.programmer, gnu.gcc.help
> >
> > Hello,
> >
> > I'm using gcc's erand48.  On solaris8, the man page header
> > says SunOS5.8, while on cygwin, it says NEWLIB.  Both
> > pages say that the range of random numbers is the
> > closed range [0.0,1.0].  However, "many people" have
> > said on various web pages that the values lie in the
> > range [0.0,1.0) i.e. doesn't include 1.0. This is kind of
> > important if you multiply the random number by SomeInt+1
> > and and take the floor().  In the former case, you have the
> > slight possibility of getting SomeInt+1, which is bad if
> > SomeInt represents the index of the final element in an array.
> >
> > So I wonder if anyone knows for sure whether the random
> > number spans the closed range, or semiopen (latter case).
> > I mean know for sure, because suspicions abound (I have
> > my own suspicions that the semiopen range is correct because
> > the integer counterparts in this family of random generators
> > always goes up to +/-( 2**31 - 1 ), whereas I suspect the
> > floating point versions just normalize the integer version by
> > 2**31.
> >
> > One could always write code to trap the case when erand48
> > returns 1.0, but that's an overhead that you pay for every
> > time you make a call to erand48.
>
> Looking at the source code, it looks like it returns "unsigned short
> xseed[3]" into 48 bits via
>
>   ldexp((double) xseed[0],-48) + ldexp((double) xseed[1],-32) +
>    ldexp((double) xseed[2], -16);
>
> which would imply that it CANNOT exactly equal one.
>
> This isn't the gcc version--I'm looking at the code from
>
>
> <http://www.ics.uci.edu/~eppstein/projects/pairs/Source/testbed/rand48/>
>
> --but I doubt there's much difference.

That's very cool.  Thanks.  Effectively, it's using a random collection
of bits to generate an integer in [0,2**48-1] and then moving the
decimal point (er...I mean binary point) all the way to the left.

Fred

--
Fred Ma, fma@doe.carleton.ca
Carleton University, Dept. of Electronics
1125 Colonel By Drive, Ottawa, Ontario
Canada, K1S 5B6
Technical Details
Message-ID: <3E682272.AC29202B@doe.carleton.ca>
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!newsfeed.stanford.edu!nntp.cs.ubc.ca!torn!cunews!not-for-mail

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 11318c,fb356e03a291e1e4
X-Google-Attributes: gid11318c,public
X-Google-Thread: fc89c,772ea27ce7480c6b
X-Google-Attributes: gidfc89c,public
X-Google-Thread: 107079,8ae001fa56b91764
X-Google-Attributes: gid107079,public
X-Google-ArrivalTime: 2003-03-06 20:40:03 PST
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!newsfeed.stanford.edu!nntp.cs.ubc.ca!torn!cunews!not-for-mail
From: Shing-Fat Fred Ma <fma@doe.carleton.ca>
Newsgroups: comp.unix.programmer,sci.math.num-analysis,gnu.gcc.help
Subject: Re: Can erand48 really generate a 1.0?
Date: 7 Mar 2003 04:39:17 GMT
Organization: Carleton University, Ottawa, Canada
Lines: 69
Message-ID: <3E682272.AC29202B@doe.carleton.ca>
NNTP-Posting-Host: malawi.doe.carleton.ca
Mime-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
X-Mailer: Mozilla 4.77 [en] (X11; U; SunOS 5.8 sun4u)
X-Accept-Language: en