POV-Ray : Newsgroups : povray.binaries.images : Toward a less-lame rand() function. : Re: Toward a less-lame rand() function. Server Time
1 Aug 2024 00:23:19 EDT (-0400)
  Re: Toward a less-lame rand() function.  
From: Jay Fox
Date: 20 May 2009 14:05:01
Message: <web.4a1445cc2a89ae6dd92e869d0@news.povray.org>
"Jay Fox" <jay### [at] gmailcom> wrote:
> Sorry folks, but no free lunch. Multiple rand() streams with consecutive seeds
> will show a very regular pattern. The pattern continues, regardless of how far
> we go into the stream.
>
> As a practical example, if we grabbed the first three rand() values and made
> them into vector, and we used the frame number as the seed, then the vector
> will follow a well-defined lattice pattern, i.e., it will change by the same
> amount from frame to frame, mod 1.0 on each axis to stay in the unit cube.
>
> An LCG is only useful as an RNG if we pick one and only one seed. Therefore,
> from frame to frame, we need to seed with the last rand() value from the
> previous frame. Unless, of course, we use a different RNG.

After thinking about it, I realized the situation isn't so bad after all.

Yes, taking consecutive seeds will display a very regular pattern. But this is
due to insufficient seeding, not insufficient "randomness" of the RNG.

The solution, of course, is to use better seeding. The answer so far has been to
use the last rand() value from the previous frame.

But a simpler approach would accomplish nearly the same result: use a non-linear
function of the frame number.

A simple example would be to use a polynomial of degree at least 2. For example,
if k is the frame number, then seed(k*k) or seed(k*k*k) would work very nicely.
Give it a try and see what you think. Even using the first rand() value, this
approach should provide good randomness.

Another simple approach would be to use k as the seed, but throw away the first
k random numbers, or the first k*k. Or mix and match these two approaches as
needed. For example, seed with seed(k*k*k), and throw away the first k rand()
values.


Post a reply to this message

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