POV-Ray : Newsgroups : povray.general : Random integer Server Time
16 May 2024 10:44:55 EDT (-0400)
  Random integer (Message 11 to 20 of 22)  
<<< Previous 10 Messages Goto Latest 10 Messages Next 2 Messages >>>
From: Thomas de Groot
Subject: Re: Random integer
Date: 1 Feb 2016 07:21:24
Message: <56af4dc4$1@news.povray.org>
On 1-2-2016 13:19, William F Pokorny wrote:
> On 02/01/2016 03:06 AM, Thomas de Groot wrote:
>> On 1-2-2016 3:10, clipka wrote:
>>> Am 01.02.2016 um 02:55 schrieb Mike Horvath:
>>>> How do I calculate a random integer between n and p, inclusively?
>>>
>>> floor( rand(R)*((p-n)+1) + 0.5 )
>>>
>>> should do the trick
>>>
>>
>> Why not this: int(RRand(n,p,R))
>>
> RRand(Min,Max,RandSeededStream) from rand.inc. To get the inclusive part
> of the request I think we need:
>
> int(RRand(n,p+(1-1e-6),R))
>
> or
>
> floor(RRand(n,p+(1-1e-6),R))
>
> Bill P.

Are the Min and Max not inclusive by default? I always got that impression.

-- 
Thomas


Post a reply to this message

From: William F Pokorny
Subject: Re: Random integer
Date: 1 Feb 2016 10:30:14
Message: <56af7a06$1@news.povray.org>
On 02/01/2016 07:21 AM, Thomas de Groot wrote:
> On 1-2-2016 13:19, William F Pokorny wrote:
>> On 02/01/2016 03:06 AM, Thomas de Groot wrote:
>>> On 1-2-2016 3:10, clipka wrote:
>>>> Am 01.02.2016 um 02:55 schrieb Mike Horvath:
>>>>> How do I calculate a random integer between n and p, inclusively?
>>>>
>>>> floor( rand(R)*((p-n)+1) + 0.5 )
>>>>
>>>> should do the trick
>>>>
>>>
>>> Why not this: int(RRand(n,p,R))
>>>
>> RRand(Min,Max,RandSeededStream) from rand.inc. To get the inclusive part
>> of the request I think we need:
>>
>> int(RRand(n,p+(1-1e-6),R))
>>
>> or
>>
>> floor(RRand(n,p+(1-1e-6),R))
>>
>> Bill P.
>
> Are the Min and Max not inclusive by default? I always got that impression.
>
For floats yes.

Mike wants integers so we are using int() or floor(). Seems to me the 
only way we can get the Max integer value is if the rand() function 
returns exactly 1.0 - which it will pretty much never do, but might.

The +(1-1e-6) makes it so we get the Max integer value only one 
millionth less often than ideally we should.

We could specify a max integer 1 larger than we really want for p, but 
then rand() will be a troublemaker and give us that 1.0 value in our 
scene.

Bill P.


Post a reply to this message

From: Thomas de Groot
Subject: Re: Random integer
Date: 2 Feb 2016 02:42:08
Message: <56b05dd0$1@news.povray.org>
On 1-2-2016 16:30, William F Pokorny wrote:
> On 02/01/2016 07:21 AM, Thomas de Groot wrote:
>> On 1-2-2016 13:19, William F Pokorny wrote:
>>> On 02/01/2016 03:06 AM, Thomas de Groot wrote:
>>>> On 1-2-2016 3:10, clipka wrote:
>>>>> Am 01.02.2016 um 02:55 schrieb Mike Horvath:
>>>>>> How do I calculate a random integer between n and p, inclusively?
>>>>>
>>>>> floor( rand(R)*((p-n)+1) + 0.5 )
>>>>>
>>>>> should do the trick
>>>>>
>>>>
>>>> Why not this: int(RRand(n,p,R))
>>>>
>>> RRand(Min,Max,RandSeededStream) from rand.inc. To get the inclusive part
>>> of the request I think we need:
>>>
>>> int(RRand(n,p+(1-1e-6),R))
>>>
>>> or
>>>
>>> floor(RRand(n,p+(1-1e-6),R))
>>>
>>> Bill P.
>>
>> Are the Min and Max not inclusive by default? I always got that
>> impression.
>>
> For floats yes.
>
> Mike wants integers so we are using int() or floor(). Seems to me the
> only way we can get the Max integer value is if the rand() function
> returns exactly 1.0 - which it will pretty much never do, but might.
>
> The +(1-1e-6) makes it so we get the Max integer value only one
> millionth less often than ideally we should.
>
> We could specify a max integer 1 larger than we really want for p, but
> then rand() will be a troublemaker and give us that 1.0 value in our scene.
>
> Bill P.
>

OK. Got it. Thanks a lot!

-- 
Thomas


Post a reply to this message

From: Mike Horvath
Subject: Re: Random integer
Date: 4 Feb 2016 21:42:42
Message: <56b40c22$1@news.povray.org>
On 1/31/2016 9:08 PM, Ger wrote:
> Ger wrote:
>
>> Mike Horvath wrote:
>>
>>> How do I calculate a random integer between n and p, inclusively?
>>>
>>>
>>> Thanks
>> #declare RSeed = seed(1);
>>
> #declare RandomNumber = int(n + rand(RSeed) * (p - n));
>


Shouldn't you use floor instead of int? What if n and p are both  negative?


Mike


Post a reply to this message

From: Mike Horvath
Subject: Re: Random integer
Date: 4 Feb 2016 21:45:53
Message: <56b40ce1$1@news.povray.org>
On 1/31/2016 11:27 PM, Sven Littkowski wrote:
> Yes, there are two functions to convert a float to a close-by integer:
>
> 1. Floor
> If the float is, let's say, "11.4676475" (could be a multiplication of a
> "rand" random result), floor returns "11" (the next integer BELOW that
> float).
>
> 2. Ceil(ing)
> If the float is, let's say, "11.4676475" (could be a multiplication of a
> "rand" random result), floor returns "12" (the next integer ABOVE that
> float).
>


There is also int, which varies depending on if the number is greater or 
less than zero.


Post a reply to this message

From: Mike Horvath
Subject: Re: Random integer
Date: 4 Feb 2016 21:49:26
Message: <56b40db6$1@news.povray.org>
On 2/1/2016 10:30 AM, William F Pokorny wrote:
> On 02/01/2016 07:21 AM, Thomas de Groot wrote:
>> On 1-2-2016 13:19, William F Pokorny wrote:
>>> On 02/01/2016 03:06 AM, Thomas de Groot wrote:
>>>> On 1-2-2016 3:10, clipka wrote:
>>>>> Am 01.02.2016 um 02:55 schrieb Mike Horvath:
>>>>>> How do I calculate a random integer between n and p, inclusively?
>>>>>
>>>>> floor( rand(R)*((p-n)+1) + 0.5 )
>>>>>
>>>>> should do the trick
>>>>>
>>>>
>>>> Why not this: int(RRand(n,p,R))
>>>>
>>> RRand(Min,Max,RandSeededStream) from rand.inc. To get the inclusive part
>>> of the request I think we need:
>>>
>>> int(RRand(n,p+(1-1e-6),R))
>>>
>>> or
>>>
>>> floor(RRand(n,p+(1-1e-6),R))
>>>
>>> Bill P.
>>
>> Are the Min and Max not inclusive by default? I always got that
>> impression.
>>
> For floats yes.
>
> Mike wants integers so we are using int() or floor(). Seems to me the
> only way we can get the Max integer value is if the rand() function
> returns exactly 1.0 - which it will pretty much never do, but might.
>
> The +(1-1e-6) makes it so we get the Max integer value only one
> millionth less often than ideally we should.
>
> We could specify a max integer 1 larger than we really want for p, but
> then rand() will be a troublemaker and give us that 1.0 value in our scene.
>
> Bill P.
>

So should I use int or floor? The result is different for numbers below 
zero.


Mike


Post a reply to this message

From: Mike Horvath
Subject: Re: Random integer
Date: 4 Feb 2016 21:50:15
Message: <56b40de7$1@news.povray.org>
On 1/31/2016 10:16 PM, clipka wrote:
> Am 01.02.2016 um 03:10 schrieb clipka:
>> Am 01.02.2016 um 02:55 schrieb Mike Horvath:
>>> How do I calculate a random integer between n and p, inclusively?
>>
>> floor( rand(R)*((p-n)+1) + 0.5 )
>>
>> should do the trick
>
> ... provided you figure out that you need to add n ;)
>
> Oops.
>

Where do I add n?


Post a reply to this message

From: William F Pokorny
Subject: Re: Random integer
Date: 4 Feb 2016 22:53:30
Message: <56b41cba$1@news.povray.org>
On 02/04/2016 09:49 PM, Mike Horvath wrote:
> On 2/1/2016 10:30 AM, William F Pokorny wrote:
>
> So should I use int or floor? The result is different for numbers below
> zero.
>
> Mike

Good question. If you have negative integers as part of your potential 
integer range, use floor().

Aside: In some languages to really get an integer you need int() too as 
in : int(floor(...)) because ceil() and floor() in those languages 
return floats.

Bill P.


Post a reply to this message

From: clipka
Subject: Re: Random integer
Date: 5 Feb 2016 02:51:55
Message: <56b4549b$1@news.povray.org>
Am 05.02.2016 um 03:50 schrieb Mike Horvath:
> On 1/31/2016 10:16 PM, clipka wrote:
>> Am 01.02.2016 um 03:10 schrieb clipka:
>>> Am 01.02.2016 um 02:55 schrieb Mike Horvath:
>>>> How do I calculate a random integer between n and p, inclusively?
>>>
>>> floor( rand(R)*((p-n)+1) + 0.5 )
>>>
>>> should do the trick
>>
>> ... provided you figure out that you need to add n ;)
>>
>> Oops.
>>
> 
> Where do I add n?

Never mind -- I also included other nonsense in the formula.

Let me try that again:

    min( n+floor( rand(R)*((p-n)+1) ), p )

The min() is there to ensure that the result is never larger than p,
even if R is so close to 1.0 that the expression in floor() is at some
point rounded up to give a result of (p-n)+1, in which case the entire
expression (without the min()) would result in p+1.

I'm adding n only after rounding, because the lower the magnitude of a
floating point number, the higher its absolute precision.


Post a reply to this message

From: clipka
Subject: Re: Random integer
Date: 5 Feb 2016 02:57:10
Message: <56b455d6$1@news.povray.org>
Am 05.02.2016 um 03:49 schrieb Mike Horvath:
> So should I use int or floor? The result is different for numbers below
> zero.

In your case you want floor(); otherwise, if your interval includes 0,
too many random numbers will be mapped to 0, and not enough random
numbers will be mapped to 0.


Post a reply to this message

<<< Previous 10 Messages Goto Latest 10 Messages Next 2 Messages >>>

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