POV-Ray : Newsgroups : povray.general : Random integer Server Time
23 Dec 2024 12:39:57 EST (-0500)
  Random integer (Message 1 to 10 of 22)  
Goto Latest 10 Messages Next 10 Messages >>>
From: Mike Horvath
Subject: Random integer
Date: 31 Jan 2016 20:55:21
Message: <56aebb09$1@news.povray.org>
How do I calculate a random integer between n and p, inclusively?


Thanks


Post a reply to this message

From: Ger
Subject: Re: Random integer
Date: 31 Jan 2016 21:06:23
Message: <56aebd9f@news.povray.org>
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);
-- 

Ger


Post a reply to this message

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

Goto Latest 10 Messages Next 10 Messages >>>

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