POV-Ray : Newsgroups : povray.general : Random integer Server Time
2 Jun 2024 15:02:25 EDT (-0400)
  Random integer (Message 3 to 12 of 22)  
<<< Previous 2 Messages Goto Latest 10 Messages Next 10 Messages >>>
From: Ger
Subject: Re: Random integer
Date: 31 Jan 2016 21:08:58
Message: <56aebe3a$1@news.povray.org>
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));

-- 

Ger


Post a reply to this message

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

That results in a float, not an integer.


Mike


Post a reply to this message

From: clipka
Subject: Re: Random integer
Date: 31 Jan 2016 21:10:16
Message: <56aebe88@news.povray.org>
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


Post a reply to this message

From: Ger
Subject: Re: Random integer
Date: 31 Jan 2016 21:13:54
Message: <56aebf62$1@news.povray.org>
Mike Horvath wrote:

> On 1/31/2016 9:06 PM, Ger wrote:
>> Mike Horvath wrote:
>>
>>> How do I calculate a random integer between n and p, inclusively?
>>>
>>>
>>> Thanks
>> #declare RSeed = seed(1);
>>
>> #declare RandomNumber = n + rand(RSeed) * (p - n);
>>
> 
> That results in a float, not an integer.
> 
> 
> Mike

I corrected it.
-- 

Ger


Post a reply to this message

From: clipka
Subject: Re: Random integer
Date: 31 Jan 2016 22:17:13
Message: <56aece39@news.povray.org>
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.


Post a reply to this message

From: Sven Littkowski
Subject: Re: Random integer
Date: 31 Jan 2016 23:28:05
Message: <56aeded5$1@news.povray.org>
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).


Post a reply to this message

From: Thomas de Groot
Subject: Re: Random integer
Date: 1 Feb 2016 03:06:33
Message: <56af1209$1@news.povray.org>
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))

-- 
Thomas


Post a reply to this message

From: William F Pokorny
Subject: Re: Random integer
Date: 1 Feb 2016 07:19:14
Message: <56af4d42$1@news.povray.org>
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.


Post a reply to this message

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

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

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