POV-Ray : Newsgroups : povray.general : rand question : Re: rand question Server Time
5 Aug 2024 02:19:19 EDT (-0400)
  Re: rand question  
From: Christopher James Huff
Date: 12 Feb 2003 16:40:42
Message: <cjameshuff-F016FD.16403212022003@netplex.aussie.org>
In article <3e4a9fe0@news.povray.org>, Warp <war### [at] tagpovrayorg> 
wrote:

>   In the man page of rand() in some Unix variant (don't remember which)
> there was a note that the lowest bit of the number returned by rand() should
> not be used because it alternates between 0 and 1 in successive calls.
> 
>   Makes one wonder if it wouldn't be better to fix the problem instead
> of documenting it... :)

Maybe there was some other constraint that kept them from doing 
so...performance, etc. Or maybe they just didn't know anything about 
PRNG's.

In any case, I never use rand(). I occasionally use random(). From the 
manpage:

     The random() function uses a non-linear additive feedback random 
     number generator employing a default table of size 31 long 
     integers to return successive pseudo-random numbers in the range 
     from 0 to (2**31)-1.  The period of this random number generator 
     is very large, approximately 16*((2**31)-1).

     The random() and srandom() functions have (almost) the same 
     calling sequence and initialization properties as the rand(3) and 
     srand(3) functions.  The difference is that rand(3) produces a 
     much less random sequence -- in fact, the low dozen bits generated 
     by rand go through a cyclic pattern.  All the bits generated by 
     random() are usable.  For example, `random()&01' will produce a 
     random binary value.

It is pretty simple to create a wrapper class for it. Usually I use a 
Mersenne Twister class I wrote. (Meaning of course that I wrote the 
class, much of the code is very close to the original implementation by 
Makoto Matsumoto and Takuji Nishimura.)
This is the random number generator I used in Sapphire.

-- 
Christopher James Huff <cja### [at] earthlinknet>
http://home.earthlink.net/~cjameshuff/
POV-Ray TAG: chr### [at] tagpovrayorg
http://tag.povray.org/


Post a reply to this message

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.