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:21:32 EDT (-0400)
  Re: Toward a less-lame rand() function.  
From: Warp
Date: 21 May 2009 03:59:07
Message: <4a1509cb$1@news.povray.org>
Kenneth wrote:
> If my simplistic assumptions/understandings are correct, then it seems to me
> that it all still begs the question of why there are repetitive patterns when
> using seed(frame_number) in animations--albeit when calling only one rand()
> value per seed. I don't mean to beat this poor old horse to death, but
> seed(frame number 23) and seed(frame number 24) should--as far as I understand
> things--pick up the 2^32 stream at wildly differing locations

  They do choose a starting point at "wildly different locations".
That's not the issue. The issue is that there's a discernible pattern
between seed(23), seed(24) and seed(25). They all jump around the stream
rather "wildly", but by the same amount. Basically seed(24) has the same
"difference" to seed(23), as seed(25) has to seed(24). That's where the
regular patterns are so easily jumping out.

  This is due to how a linear congruential generator works. As
mentioned, the next value in the stream is chosen with the formula:

n = n * a + b; // (modulo 2^32, as 32-bit integers are used)

where a and b are some constants (and the initial value of n is the seed
you specified).

  So if you calculate that with consecutive values of n (which is what
you are doing with seed(frame_number)), what you are getting is
consecutive multiples of a, with an offset of b (modulo 2^32).

  One thing you could try to avoid the regularity is to call rand() a
random number of times at each frame. This might mask the regularity
between consecutive seeds. For example like this:

#declare S = seed(frame_number);
#declare Ind = 0;
#declare IndMax = int(rand(S)*10);
#while(Ind < IndMax)
  #declare DummyValue = rand(S);
  #declare Ind = Ind+1;
#end

  Since you are choosing the amount of loops with the same RNG, the
overall quality of the randomness is not increasing, but this trick
might mask the regular patterns, so they won't become discernible so easily.


Post a reply to this message

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