POV-Ray : Newsgroups : povray.documentation.inbuilt : revisiting rand(...) : Re: revisiting rand(...) Server Time
17 Oct 2021 22:38:09 EDT (-0400)
  Re: revisiting rand(...)  
From: Alain
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

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