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:24:50 EDT (-0400)
  Re: Toward a less-lame rand() function.  
From: Warp
Date: 18 May 2009 16:06:12
Message: <4a11bfb4@news.povray.org>
Cousin Ricky wrote:
> All that means is that POV uses better
> LCG parameters than IBM did.

  I'm pretty certain that's not the case, and that the difference you
are seeing is caused by your choice of bits in the returned values,
rather than the quality of the LCG.

  LCGs have one big flaw: If you take the lowest bits, they tend to show
regular patterns. For example, doing something like this (in pseudocode):

    putpixel(rand() % 256, rand() % 256)

is *very* likely to produce visible regular patterns. That's because the
least-significant 8 bits of the values returned by the RNG are used
as-is to generate the locations of the pixels.

  A simple change to the above code like this:

    putpixel((rand() / 65536) % 256, (rand() / 65536) % 256)

is probably going to get rid of the regular pattern, because now it will
be using bits 16-23 rather than bits 0-7, and the higher bits in a LCG
usually don't present the same type of regularity as the lowest bits.

  The reason why in POV-Ray you rarely get the regular patterns is that
you are usually using the highest bits of the values returned by rand(),
rather than the lowest bits. That's because POV-Ray's rand() returns
seed/4294967296.0 (which effectively scales the 32-bit seed to the range
0.0 - 1.0). Since the value of rand() is then usually multiplied by
something to place objects (eg. rand()*256), you are effectively using
the highest bits of the value rather than the lowest bits, which is why
it's rarer to get the repeated patterns.

  The *quality* of POV-Ray's RNG is not any higher. It's just that you
are not encountering the flaws in the LCG so easily.


Post a reply to this message

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