POV-Ray : Newsgroups : povray.documentation.inbuilt : revisiting rand(...) Server Time
21 Dec 2024 22:16:29 EST (-0500)
  revisiting rand(...) (Message 8 to 17 of 17)  
<<< Previous 7 Messages Goto Initial 10 Messages
From: Jim Holsenback
Subject: Re: revisiting rand(...)
Date: 18 Jul 2018 04:54:13
Message: <5b4f0035$1@news.povray.org>
On 07/17/2018 06:27 AM, Kenneth wrote:
> (BTW, I tried to find the rand() entry in the online wiki, just to see if the
> info there has changed since the latest(?) POV-included documentation, but I
> didn't know where to look.)

most of the time i look here first:
http://wiki.povray.org/content/Reference:Keywords


Post a reply to this message

From: clipka
Subject: Re: revisiting rand(...)
Date: 18 Jul 2018 09:18:43
Message: <5b4f3e33$1@news.povray.org>
Am 17.07.2018 um 22:11 schrieb Kenneth:
> "Bald Eagle" <cre### [at] netscapenet> wrote:
>> Alain <kua### [at] videotronca> wrote:
>>
>>> All values are equally possible, including 0 and 1. The mantissa for a
>>> float is about 52 bits IIRC, so, you have a 1 in 2^52 chances of getting
>>> a 0 and the same of getting a 1. Very small but not zero chance.
>>
>> So then maybe round to the nearest Q decimal place to "compress" the range and
>> increase the likelihood of getting a 1 or a 0.
>>
> 
> Compressing the range is an interesting concept, that I wasn't aware of. I'll
> give some thought to the formula presented there. But I would suspect that
> POV-Ray's internal rounding would be *either* 'round up' OR 'round down', but
> not both (?) In other words, a 1.0 (but never a 0.0), OR a 0.0 (but never a
> 1.0).

While that's indeed the way most floating-point random number generators
work -- usually giving you a number ranging from 0.0 inclusive to 1.0
exclusive -- POV-Ray's SDL random number generator is designed to be
inclusive both fenceposts.


Post a reply to this message

From: Kenneth
Subject: Re: revisiting rand(...)
Date: 18 Jul 2018 10:40:01
Message: <web.5b4f50e714e7ffaca47873e10@news.povray.org>
Jim Holsenback <ash### [at] nospamcom> wrote:
> On 07/17/2018 06:27 AM, Kenneth wrote:
> > (BTW, I tried to find the rand() entry in the online wiki, just to see if the
> > info there has changed since the latest(?) POV-included documentation, but I
> > didn't know where to look.)
>
> most of the time i look here first:
> http://wiki.povray.org/content/Reference:Keywords

Well, that's strange (and it does indeed have info on "rand"). But I originally
looked at a different "keywords" page...

http://wiki.povray.org/content/Reference:Keywords

Clicking on "rand" there brings up no actual information, just the word itself.
:-(

The wiki page that I usually look at first, as a general starting place, is...

http://wiki.povray.org/content/Documentation:Contents

 ... and I see now that it does indeed have the correct link to "identifiers and
keywords"-- but that link is located in the first paragraph of the page, which I
didn't originally notice :-O  I think a better place for that link would be in
the left-hand column of the page (with the other entries there), rather than in
the body of the page. And perhaps there's some confusing redundancy of 'keyword'
pages in the wiki?


Post a reply to this message

From: Alain
Subject: Re: revisiting rand(...)
Date: 18 Jul 2018 12:23:45
Message: <5b4f6991$1@news.povray.org>
Le 18-07-17 à 17:25, Kenneth a écrit :
> clipka <ano### [at] anonymousorg> wrote:
> 
>>
>> 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.
>>
> 
> I do see your point (and Alain's): Let's say I write some code where I naively
> *expect* a rand() value of 0.0 or 1.0 to pop up every now and then (well, with a
> 1-in-4-billion chance, assuming *equal* probability.) But those are distinct,
> specific values-- it would be like expecting the exact value of .4729418830...
> to pop up now and again. Which is highly unlikely (but still possible.)
> 
> But there's a practical aspect to consider, when writing a typical scene using
> rand()-- if my own experience is any indication:
> 
> Let's say I want to make 10,000,000 stars in the sky. My own current way of
> doing this (simplified) would be...
> 
> union{
> #for(i,1,10000000)
> sphere{0,1 scale 100*rand(R)
> translate ...
> }
> #end
>    texture{...}
> }
> 
> .... and I would ignore the *remote* possibility of rand() being 0.0, and thus
> scale being <0.0,0.0,0.0>, which would trigger an error. Yet this *seems* to be
> the behavior of rand(), in a practical sense, so I always feel safe in ignoring
> that possibility.
> 
> But to be extra-safe, the code *should* be something like this...
> ....
> sphere{0,1 scale 100*(rand(R) + "some tiny positive value")
> ....
> 
> .... even though rand() 'never' hits exactly 0.0, in my experience.
> 
> 
> 
> 
> 

If you get scale 0, a warning is generated and the scalling will default 
to scale 1.

So, no error, just a warning.


Alain


Post a reply to this message

From: clipka
Subject: Re: revisiting rand(...)
Date: 18 Jul 2018 18:55:31
Message: <5b4fc563$1@news.povray.org>
Am 18.07.2018 um 16:38 schrieb Kenneth:
> Jim Holsenback <ash### [at] nospamcom> wrote:
>> On 07/17/2018 06:27 AM, Kenneth wrote:
>>> (BTW, I tried to find the rand() entry in the online wiki, just to see if the
>>> info there has changed since the latest(?) POV-included documentation, but I
>>> didn't know where to look.)
>>
>> most of the time i look here first:
>> http://wiki.povray.org/content/Reference:Keywords
> 
> Well, that's strange (and it does indeed have info on "rand"). But I originally
> looked at a different "keywords" page...
> 
> http://wiki.povray.org/content/Reference:Keywords

I guess you mean `http://wiki.povray.org/content/Keyword`.

> Clicking on "rand" there brings up no actual information, just the word itself.
> :-(

Yeah. The entire "Keyword" section of the Wiki (as opposed to the
"Reference:Keywords" page) is, to the best of my knowledge, only there
for technical reasons, related to some age-old bad decision that nobody
had the time or energy to roll back yet.


Post a reply to this message

From: Kenneth
Subject: Re: revisiting rand(...)
Date: 18 Jul 2018 19:00:01
Message: <web.5b4fc57614e7ffaca47873e10@news.povray.org>
Alain <kua### [at] videotronca> wrote:

>
> If you get scale 0, a warning is generated and the scalling will default
> to scale 1.
>
> So, no error, just a warning.
>

That's true; I probably used a poor code example to demonstrate what might occur
IF rand(...) actually hit 0.0


Post a reply to this message

From: Bald Eagle
Subject: Re: revisiting rand(...)
Date: 18 Jul 2018 20:05:00
Message: <web.5b4fd52214e7ffac458c7afe0@news.povray.org>
"Kenneth" <kdw### [at] gmailcom> wrote:
> "Bald Eagle" <cre### [at] netscapenet> wrote:
> > Alain <kua### [at] videotronca> wrote:
> >
> > > All values are equally possible, including 0 and 1. The mantissa for a
> > > float is about 52 bits IIRC, so, you have a 1 in 2^52 chances of getting
> > > a 0 and the same of getting a 1. Very small but not zero chance.
> >
> > So then maybe round to the nearest Q decimal place to "compress" the range and
> > increase the likelihood of getting a 1 or a 0.
> >
>
> Compressing the range is an interesting concept, that I wasn't aware of. I'll
> give some thought to the formula presented there. But I would suspect that
> POV-Ray's internal rounding would be *either* 'round up' OR 'round down', but
> not both (?) In other words, a 1.0 (but never a 0.0), OR a 0.0 (but never a
> 1.0).

I did find the topic of rounding to be more complex than my initial naive
concept.  There are different ways to do it, and it would be useful and
instructive to see examples of code that performed all the different variations.

Likewise, it would likely be useful for people writing code that requires
variations of inclusive / exclusive bounds to be able to

(0, 5) = 1, 2, 3, 4
(0, 5] = 1, 2, 3, 4, 5
[0, 5) = 0, 1, 2, 3, 4
[0, 5] = 0, 1, 2, 3, 4, 5


It would also be informative to know just what the probability of any given rand
() result is, given the starting arguments / formulation.


Post a reply to this message

From: Alain
Subject: Re: revisiting rand(...)
Date: 18 Jul 2018 21:38:00
Message: <5b4feb78@news.povray.org>
Le 18-07-18 à 20:02, Bald Eagle a écrit :
> "Kenneth" <kdw### [at] gmailcom> wrote:
>> "Bald Eagle" <cre### [at] netscapenet> wrote:
>>> Alain <kua### [at] videotronca> wrote:
>>>
>>>> All values are equally possible, including 0 and 1. The mantissa for a
>>>> float is about 52 bits IIRC, so, you have a 1 in 2^52 chances of getting
>>>> a 0 and the same of getting a 1. Very small but not zero chance.
>>>
>>> So then maybe round to the nearest Q decimal place to "compress" the range and
>>> increase the likelihood of getting a 1 or a 0.
>>>
>>
>> Compressing the range is an interesting concept, that I wasn't aware of. I'll
>> give some thought to the formula presented there. But I would suspect that
>> POV-Ray's internal rounding would be *either* 'round up' OR 'round down', but
>> not both (?) In other words, a 1.0 (but never a 0.0), OR a 0.0 (but never a
>> 1.0).
> 
> I did find the topic of rounding to be more complex than my initial naive
> concept.  There are different ways to do it, and it would be useful and
> instructive to see examples of code that performed all the different variations.

Mostly, there are 3 ways : Round down, or truncate, round to the closest 
and round up.

Using int() or floor() is useful to round down.
Using ceil() is the way to round up.
Adding half of the desired interval and using int() or floor() or 
subtract half the range and use int() or ceil() to round to the closest.
The advantage of floor() and ceil() is that it's easier to round to 
non-integer bounds.

> 
> Likewise, it would likely be useful for people writing code that requires
> variations of inclusive / exclusive bounds to be able to
> 
> (0, 5) = 1, 2, 3, 4
> (0, 5] = 1, 2, 3, 4, 5
> [0, 5) = 0, 1, 2, 3, 4
> [0, 5] = 0, 1, 2, 3, 4, 5
> 
> 
> It would also be informative to know just what the probability of any given rand
> () result is, given the starting arguments / formulation.
> 
> 

Various seed() should all result in the same probabilities, but with 
various sequences.


Post a reply to this message

From: Alain
Subject: Re: revisiting rand(...)
Date: 18 Jul 2018 21:39:52
Message: <5b4febe8$1@news.povray.org>
Le 18-07-18 à 18:55, Kenneth a écrit :
> Alain <kua### [at] videotronca> wrote:
> 
>>
>> If you get scale 0, a warning is generated and the scalling will default
>> to scale 1.
>>
>> So, no error, just a warning.
>>
> 
> That's true; I probably used a poor code example to demonstrate what might occur
> IF rand(...) actually hit 0.0
> 
> 

#declare Something = Value/rand(R);

Crash with divide by zero if rand(R) return zero.


Post a reply to this message

From: clipka
Subject: Re: revisiting rand(...)
Date: 19 Jul 2018 11:58:56
Message: <5b50b540$1@news.povray.org>
Am 19.07.2018 um 03:38 schrieb Alain:

>> I did find the topic of rounding to be more complex than my initial naive
>> concept.  There are different ways to do it, and it would be useful and
>> instructive to see examples of code that performed all the different
>> variations.
> 
> Mostly, there are 3 ways : Round down, or truncate, round to the closest
> and round up.

Actually there are a few more commonly used:

- Round towards negative infinity (round down)
- Round towards positive infinity (round up)
- Round towards zero (truncate)
- Round towards infinity of same sign
- Round to nearest ("proper" rounding)

In the latter mode, there is again a host of ways to handle values smack
in the middle between the nearest representable numbers (think 0.5):

- Round towards negative infinity (round down)
- Round towards positive infinity (round up)
- Round towards zero (truncate)
- Round towards infinity of same sign
- Round up if even, down if odd
- Round down if even, up if odd


Post a reply to this message

<<< Previous 7 Messages Goto Initial 10 Messages

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