POV-Ray : Newsgroups : povray.newusers : Need rand() help Server Time
25 Nov 2024 01:41:25 EST (-0500)
  Need rand() help (Message 1 to 7 of 7)  
From: George J  Wallace
Subject: Need rand() help
Date: 8 Jan 2007 20:42:32
Message: <45a2f308$1@news.povray.org>
I'm trying to generate 3 random numbers to add to a base RGB color value but
I am totally lost trying to use rand().  I haven't had a math class since
high school and I don't even understand the help file entry.  Could someone
point me at a tutorial or a few lines of code that might show me how to use
this function properly.  TIA...gjw

--


Post a reply to this message

From: Slime
Subject: Re: Need rand() help
Date: 8 Jan 2007 23:04:12
Message: <45a3143c@news.povray.org>
#declare colorSeed = seed(5454353); // (use any number you want, change it
to change the randomness)
// rand(...) returns a random number between 0 and 1.
#declare myColor = < rand(colorSeed), rand(colorSeed), rand(colorSeed) >;

 - Slime
 [ http://www.slimeland.com/ ]


Post a reply to this message

From: George J  Wallace
Subject: Re: Need rand() help
Date: 8 Jan 2007 23:32:40
Message: <45a31ae8$1@news.povray.org>
Thanks for the fast reply.  Is the value plugged into the rand() statement
actually a subscript to a value in the random number stream?  If yes, then
how long is the random number stream (how many times can I tap into the same
stream)? TIA, again...gjw
"Slime" <fak### [at] emailaddress> wrote in message
news:45a3143c@news.povray.org...
> #declare colorSeed = seed(5454353); // (use any number you want, change it
> to change the randomness)
> // rand(...) returns a random number between 0 and 1.
> #declare myColor = < rand(colorSeed), rand(colorSeed), rand(colorSeed) >;
>
>  - Slime
>  [ http://www.slimeland.com/ ]
>
>


Post a reply to this message

From: Slime
Subject: Re: Need rand() help
Date: 8 Jan 2007 23:37:52
Message: <45a31c20$1@news.povray.org>
> Thanks for the fast reply.  Is the value plugged into the rand() statement
> actually a subscript to a value in the random number stream?  If yes, then
> how long is the random number stream (how many times can I tap into the
same
> stream)? TIA, again...gjw


Think of colorSeed as a list of random numbers, and rand(colorSeed) always
gets the next one. If you want to start over, you just #declare colorSeed
again in the same way. My guess is that the stream is about 2^32 numbers
long, which is long enough that you'll never hit the end of it (and if you
do it will just loop back to the beginning).

The only reason to have the "seed" in the first place is so that you can
have different streams of random numbers. So if you write code that
generates 100 random colors, and you like the randomness you're getting, but
in the same loop you want to generate 100 random positions, you can use a
different seed (like positionSeed or something) and it won't change the
colors that are generated.

 - Slime
 [ http://www.slimeland.com/ ]


Post a reply to this message

From: George J  Wallace
Subject: Re: Need rand() help
Date: 9 Jan 2007 09:48:14
Message: <45a3ab2e$1@news.povray.org>
Thank you Very Much.  I was trying to use the value in rand() like a
subscript and it kept spitting up.  Thanks again for the explanation...gjw
"Slime" <fak### [at] emailaddress> wrote in message
news:45a31c20$1@news.povray.org...
> > Thanks for the fast reply.  Is the value plugged into the rand()
statement
> > actually a subscript to a value in the random number stream?  If yes,
then
> > how long is the random number stream (how many times can I tap into the
> same
> > stream)? TIA, again...gjw
>
>
> Think of colorSeed as a list of random numbers, and rand(colorSeed) always
> gets the next one. If you want to start over, you just #declare colorSeed
> again in the same way. My guess is that the stream is about 2^32 numbers
> long, which is long enough that you'll never hit the end of it (and if you
> do it will just loop back to the beginning).
>
> The only reason to have the "seed" in the first place is so that you can
> have different streams of random numbers. So if you write code that
> generates 100 random colors, and you like the randomness you're getting,
but
> in the same loop you want to generate 100 random positions, you can use a
> different seed (like positionSeed or something) and it won't change the
> colors that are generated.
>
>  - Slime
>  [ http://www.slimeland.com/ ]
>
>


Post a reply to this message

From: Larry Hudson
Subject: Re: Need rand() help
Date: 9 Jan 2007 22:33:34
Message: <45a45e8e$1@news.povray.org>
George J. Wallace wrote:
> Thank you Very Much.  I was trying to use the value in rand() like a
> subscript and it kept spitting up.  Thanks again for the explanation...gjw
> "Slime" <fak### [at] emailaddress> wrote in message
> news:45a31c20$1@news.povray.org...
> 
>>  [...]
>>
>>Think of colorSeed as a list of random numbers, and rand(colorSeed) always
>>gets the next one. If you want to start over, you just #declare colorSeed
>>again in the same way. My guess is that the stream is about 2^32 numbers
>>long, which is long enough that you'll never hit the end of it (and if you
>>do it will just loop back to the beginning).
>>

Actually, it's not a 'real' list (like an array).  It's an algorithm 
that calculates a new (pseudo) random number each time it's used, based 
on the previously calculated number -- or the given seed in the case of 
the first call.

As to Slime's guess of 2^32 numbers, this would be true if the algorithm 
is based on 32-bit integers.  But since it returns floating point 
values, it may be that the algorithm is based on floating point numbers 
-- and that the total number of possible values could be much higher.  I 
don't know how the rand() function is actually implemented in POV-Ray, 
so I can't say one way or another.  Of course, as a practical matter, 
whatever it is, the number of possible values is 'enough'.

Naturally, if you want to think of this as a list or array, that's fine 
too.  It doesn't really matter how you visualize it as long as you learn 
to use it properly.

Now, a completely irrelevant side note:  I once wrote a floating point 
random number generator for my personal C-language function library. 
(The standard C function libraries already have an integer version.)
Although I'm not a good enough mathematician or statistition to really 
know the quality of my function, it was good enough for my (limited) 
uses.  (I'm just a hobby-programmer, not a professional -- and I haven't 
done much programming at all for some time.)

      -=- Larry -=-


Post a reply to this message

From: Warp
Subject: Re: Need rand() help
Date: 10 Jan 2007 03:31:57
Message: <45a4a47c@news.povray.org>
Larry Hudson <org### [at] yahoocom> wrote:
> As to Slime's guess of 2^32 numbers, this would be true if the algorithm 
> is based on 32-bit integers.

  It is, and it goes through all the possible 2^32 values. The value is
just then scales to the 0-1 range before returning.

-- 
                                                          - Warp


Post a reply to this message

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