POV-Ray : Newsgroups : povray.general : seed/rand not so random? : Re: seed/rand not so random? Server Time
29 Jul 2024 22:32:10 EDT (-0400)
  Re: seed/rand not so random?  
From: Warp
Date: 12 May 2010 13:45:02
Message: <4beae91e@news.povray.org>
stevenvh <nomail@nomail> wrote:
> Rnd1 = seed( frame_number )
> CamX = rand(Rnd1)
> CamY = rand(Rnd1)
> CamZ = rand(Rnd1)

> I noticed that the second random number (CamY) follows a rather clean linear
> function of the seed, from 0 to 1 and then returning to 0, thus resulting in a
> "sawtooth" function.

  This is a side-effect of linear congruential generators, which is what
POV-Ray uses (http://en.wikipedia.org/wiki/Linear_congruential_generator).

  As the name suggests, there's a linear relationship between the initial
seed and the subsequent values (because the RNG does nothing more than
take your seed, multiply it by a constant and add a constant, modulo the
size of unsigned int).

  Higher-quality RNGs don't usually suffer from such a clear relationship,
and adding such RNGs has been discussed.

  Currently you can try mask the problem by using some tricks. For example
you can try something like this:

#declare Rnd1 = seed(frame_number);
#declare Loops = int(rand(Rnd1)*20);
#while(Loops >= 0)
    rand(Rnd1);
    #declare Loops = Loops-1;
#end
// and then continue as normal...

  While this kind of trickery doesn't significantly improve the quality of
the randomness (and patterns might still emerge in some situations), it can
mask the effects better.

-- 
                                                          - Warp


Post a reply to this message

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