POV-Ray : Newsgroups : povray.general : Is it me or does rand(n) loop ? Server Time
5 Aug 2024 04:14:28 EDT (-0400)
  Is it me or does rand(n) loop ? (Message 1 to 9 of 9)  
From: BlackRose
Subject: Is it me or does rand(n) loop ?
Date: 18 Dec 2002 11:45:07
Message: <web.3e00a576fc9687d9f765b2b40@news.povray.org>
So I wanted to pseudorandomly place objects (with some minimum spacing)over
a 2 d field. The technique I was using for this amounted to :

k = min
while (k < max)
   l = min
   while (l < max)

       dummy = rand(x);
       if (dummy < 1/1000) & (dummy > 1/2000)
            place the object  (on average it would show up in 1 of every 2k
possible spots).
       end
   l = l+1
   end
k = k+1
end

(yeah I know it's not real pov code, but...)
Anyways, it seemed that no matter what bounds I used, I was getting square
grids of objects (instead of the randomly placed ones I wanted). Upon
debugging, I found that (atleast to 5 decimal places) the values popped out
by a particular rand stream repeated (which is not what I had expected.
BTW, I'm doing this on a winXP machine, with pov version 3.5...

Any suggestions on how to get 'round this ? Anyone else come across this
before ? I guess I could create new random number streams, but I don't know
if that is necessarily the wisest move.

Thanks...


Post a reply to this message

From: Christopher James Huff
Subject: Re: Is it me or does rand(n) loop ?
Date: 18 Dec 2002 12:41:22
Message: <chrishuff-714D01.12362618122002@netplex.aussie.org>
In article <web.3e00a576fc9687d9f765b2b40@news.povray.org>,
 "BlackRose" <nomail@nomail> wrote:

> (yeah I know it's not real pov code, but...)

Without real POV code, there is little anyone can do. What do you expect 
this to be useful for?


> Anyways, it seemed that no matter what bounds I used, I was getting square
> grids of objects (instead of the randomly placed ones I wanted). Upon
> debugging, I found that (atleast to 5 decimal places) the values popped out
> by a particular rand stream repeated (which is not what I had expected.
> BTW, I'm doing this on a winXP machine, with pov version 3.5...

Pseudorandom numbers do repeat, but the period is very large. If you are 
seeing repetition, you are probably using the rand() function wrong. 
Give the real code, trimmed down to the minimum required to demonstrate 
the problem, but check the documentation first.

-- 
Christopher James Huff <cja### [at] earthlinknet>
http://home.earthlink.net/~cjameshuff/
POV-Ray TAG: chr### [at] tagpovrayorg
http://tag.povray.org/


Post a reply to this message

From: BlackRose
Subject: Re: Is it me or does rand(n) loop ?
Date: 18 Dec 2002 13:40:03
Message: <web.3e00bfe092e939f7f765b2b40@news.povray.org>
>Pseudorandom numbers do repeat, but the period is very large. If you are
>seeing repetition, you are probably using the rand() function wrong.
>Give the real code, trimmed down to the minimum required to demonstrate
>the problem, but check the documentation first.

    Well, I think I figured out what the problem was, but not exactly why it
was a problem. It seems that I was using the same variable to define which
stream I was using (#declare a = seed(15)) in the while loop, and then
using (#declare a = seed(40)) in a macro that I used within the while loop.
By changing one to a different variable name, I seem to have fixed the
problem...

    But now I'm confused. I apologize for bothering you about this, but the
documentation wasn't very clear/understandable to me, so I was wondering
when and to what extent reusing variable names is acceptable, and how that
relates to what is going on when I define and use macros and/or other
objects. (Did that make sense ?) Also, if I call the macro repeatedly, does
the random number generator i'm using within the macro get reset each time
? What exactly goes on with the different rand-number streams when I seed
it, and later call em ?

Thanks.
Vidya


Post a reply to this message

From: Thorsten Froehlich
Subject: Re: Is it me or does rand(n) loop ?
Date: 18 Dec 2002 13:41:03
Message: <3e00c13f@news.povray.org>
In article <web.3e00a576fc9687d9f765b2b40@news.povray.org> , "BlackRose" 
<nomail@nomail> wrote:

> Anyways, it seemed that no matter what bounds I used, I was getting square
> grids of objects (instead of the randomly placed ones I wanted). Upon
> debugging, I found that (atleast to 5 decimal places) the values popped out
> by a particular rand stream repeated (which is not what I had expected.
> BTW, I'm doing this on a winXP machine, with pov version 3.5...
>
> Any suggestions on how to get 'round this ? Anyone else come across this
> before ? I guess I could create new random number streams, but I don't know

What you are seeing cannot be avoided with pure digital logic.  Thus there
is no way around it using pure digital logic; the "random" numbers will
always have a certain bias, no matter how you create them.  You can only
make them more or less close to "perfect" random numbers.  Search Google for
"random number generator" and you will get many links to almost as many
different (pseudo) random number generators.

    Thorsten


Post a reply to this message

From: Patrick Elliott
Subject: Re: Is it me or does rand(n) loop ?
Date: 18 Dec 2002 15:52:06
Message: <MPG.186a8b782f281e449896e9@news.povray.org>
In article <3e00c13f@news.povray.org>, tho### [at] trfde says...
> In article <web.3e00a576fc9687d9f765b2b40@news.povray.org> , "BlackRose" 
> <nomail@nomail> wrote:
> 
> > Anyways, it seemed that no matter what bounds I used, I was getting square
> > grids of objects (instead of the randomly placed ones I wanted). Upon
> > debugging, I found that (atleast to 5 decimal places) the values popped out
> > by a particular rand stream repeated (which is not what I had expected.
> > BTW, I'm doing this on a winXP machine, with pov version 3.5...
> >
> > Any suggestions on how to get 'round this ? Anyone else come across this
> > before ? I guess I could create new random number streams, but I don't know
> 
> What you are seeing cannot be avoided with pure digital logic.  Thus there
> is no way around it using pure digital logic; the "random" numbers will
> always have a certain bias, no matter how you create them.  You can only
> make them more or less close to "perfect" random numbers.  Search Google for
> "random number generator" and you will get many links to almost as many
> different (pseudo) random number generators.
> 
>     Thorsten
> 
Yes. Most that try to fix this use something like:

a = seed(now)

The idea being to use the current system clock time to reseed the 
generator each time. Not sure if POV has such a thing...

This can be sort of fun though if something is wrong with the system 
clock. Years ago I tried to run a dice rolling program (I.e. roll two 
dice, add them up and display what % all combos came up) on an Apple II 
who power supply turned out to be failing. When I brought it to the 
attention of the teacher he asked if I was using shaved dice. lol


Post a reply to this message

From: Anders K 
Subject: Re: Is it me or does rand(n) loop ?
Date: 18 Dec 2002 17:53:37
Message: <3e00fc71$1@news.povray.org>
Look up #local in the documentation -- in particular, section 6.2.2.2.

Anders

--
light_source{6#local D=#macro B(E)#macro A(D)#declare E=(E-#declare
C=mod(E D);C)/D;C#end#while(E)#if(A(8)=7)#declare D=D+2.8;#else#if(
C>2)}torus{1..2clipped_by{box{-2y}}rotate<1 0C>*90translate<D+1A(2)
*2+1#else}cylinder{0(C-v=1).2translate<D+C*A(2)A(4)#end-2 13>finish
{specular 1}pigment{rgb x}#end#end#end-8;1B(445000298)B(519053970)B
(483402386)B(1445571258)B(77778740)B(541684549)B(42677491)B(70)}


Post a reply to this message

From: Johannes Dahlstrom
Subject: Re: Is it me or does rand(n) loop ?
Date: 19 Dec 2002 09:56:55
Message: <3e01de37@news.povray.org>
Patrick Elliott wrote:

> The idea being to use the current system clock time to reseed the
> generator each time. Not sure if POV has such a thing...

MegaPOV has the tick_count keyword for doing that, but it can be simulated 
with POV 3.1/3.5 too: you just need a text file with a single integer in 
it. Open that file with POV, read the value and initialize a random 
generator with it. Then open the file again and write a random number to 
it. This way, every time you render your scene, you get a new seed value 
from the file.


Post a reply to this message

From: Christopher James Huff
Subject: Re: Is it me or does rand(n) loop ?
Date: 19 Dec 2002 10:33:46
Message: <chrishuff-5C1A81.10285019122002@netplex.aussie.org>
In article <3e00fc71$1@news.povray.org>,
 "Anders K." <and### [at] kaseorgcom> wrote:

> Look up #local in the documentation -- in particular, section 6.2.2.2.

#local would avoid the name collision, but not the reseeding.

-- 
Christopher James Huff <cja### [at] earthlinknet>
http://home.earthlink.net/~cjameshuff/
POV-Ray TAG: chr### [at] tagpovrayorg
http://tag.povray.org/


Post a reply to this message

From: Christopher James Huff
Subject: Re: Is it me or does rand(n) loop ?
Date: 19 Dec 2002 10:47:10
Message: <chrishuff-5D4E58.10421619122002@netplex.aussie.org>
In article <web.3e00bfe092e939f7f765b2b40@news.povray.org>,
 "BlackRose" <nomail@nomail> wrote:

>     But now I'm confused. I apologize for bothering you about this, but the
> documentation wasn't very clear/understandable to me, so I was wondering
> when and to what extent reusing variable names is acceptable, and how that
> relates to what is going on when I define and use macros and/or other
> objects. (Did that make sense ?)

Look up the documentation on local variables.


> Also, if I call the macro repeatedly, does the random number 
> generator i'm using within the macro get reset each time ? What 
> exactly goes on with the different rand-number streams when I seed 
> it, and later call em ?

I don't see why it is so confusing...you call seed() to set up a stream. 
If you do that in the macro, it gets done every time the macro is 
called, just like any other code in the macro. It isn't wrong, but it is 
almost definitely not what you want: you will always get the same string 
of numbers from a given seed value.
What you want to do is to define the stream once and then leave it alone.

-- 
Christopher James Huff <cja### [at] earthlinknet>
http://home.earthlink.net/~cjameshuff/
POV-Ray TAG: chr### [at] tagpovrayorg
http://tag.povray.org/


Post a reply to this message

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