POV-Ray : Newsgroups : povray.documentation.inbuilt : revisiting rand(...) : Re: revisiting rand(...) Server Time
13 Oct 2024 05:37:29 EDT (-0400)
  Re: revisiting rand(...)  
From: Warren
Date: 17 Jul 2018 17:25:00
Message: <web.5b4e5d7314e7ffac20df21d60@news.povray.org>
clipka <ano### [at] anonymousorg> wrote:
> Am 17.07.2018 um 12:27 schrieb Kenneth:
> > I'm wondering about the documentation for rand(...).
> >
> > It has stated for a long time that the values that rand produces are "between
> > 0.0 and 1.0, inclusive." But in all my uses of it in scenes, the value never
> > actually hits those exact values. (Some of my code would actually have a problem
> > if that happened; but I've never encountered such a problem... which is why I
> > now ignore the 'inclusivity.')
> >
> > On a whim, I ran some tests (with different seed(...) values)-- 10,000,000 tries
> > each time. But I haven't yet seen a 1.0 or a 0.0 occur.
>
> Theoretically and without too detailed analysis(*), the random number
> generator should be able to generate 2^32 (approx. 4 [short] billion)
> different results, including both 0.0 and 1.0. So unless you did your
> experiment with 200 different seeds, you're still far from the threshold
> where you could expect /any/ particular result value crop up for sure.
>
> (*The floating-point random number generator is based on an integer
> Linear Congruential Generator; there is some chance the configuration
> parameters of that LCG cause it to disfavour result values 0.0 and/or
> 1.0. However, without detailed analysis, the same could be said for any
> other of the 2^32 possible result values.)

I always thought the interval of the numbers geanrated with the rand function of
povray was between 0 (included) and 1 (excluded). That scheme would be more
relevant. Imagine you want a random integer between 0 and 4 to easily take
something in an array with an index made randomly (to avoid a switch for
example).

//Code
#declare CollectionOfThings = array[5]

#declare RandomGen = seed(789456);

#declare Thing = CollectionOfThings[int(5 * rand(RandomGen) ];

Here the index will be strictly between 0 and 5 (excluded), with a random number
between 0.0 and 0.2 (excluded) , you get an index of 0 , with a random number
between 0.2 and 0.4 (excluded) you get an index of 1 for the array, etc..

//-------------
For a random System that generate number between 0 and 1 , both included , the
above code has a bug. From 0.8 to 1.0 (excluded) you get an index of 4 and
there's still a chance of getting an index of 5 (almost null I admit it) and a
segfault. So you might think replacing the problematic code with:

#declare Thing = CollectionOfThings[int(4 * rand(RandomGen) )]

But here you have almost no chance of getting an index of 4. ;-)


Post a reply to this message

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