POV-Ray : Newsgroups : povray.off-topic : More random humous : Re: More random humous Server Time
10 Oct 2024 03:08:40 EDT (-0400)
  Re: More random humous  
From: Warp
Date: 7 Oct 2008 08:17:45
Message: <48eb5368@news.povray.org>
Invisible <voi### [at] devnull> wrote:
> You write a function that takes a PRNG state and returns a random number 
> and a new PRNG state.

  Where do you store this state, given that you can't assign it to anything?

  One common idiom in imperative languages to write a RNG function is,
for example in C++:

namespace
{
    unsigned seed = 0;

    unsigned getRandom(unsigned maxValue)
    {
        seed = <calculate a new seed using seed itself>;
        return seed % maxValue;
    }
}

  Of course you could do it like this:

unsigned getRandom(unsigned seed)
{
    return <calculate a new seed using seed itself>;
}

  But then the calling code would have to keep that seed somewhere. This
can be a real burden if the same RNG stream would need to be used in
different parts of the code (ie. basically the different parts all would
have to share the same seed).

  The idiom used in the POV-Ray SDL is that there's a seed identifier
which internally contains the seed value for that stream. However, the
rand() function *modifies* the contents of the seed identifier in order
to store the newest seed.

  I don't understand how it could be done if modifying the identifier
was prohibited.

-- 
                                                          - Warp


Post a reply to this message

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