POV-Ray : Newsgroups : povray.newusers : Using rand(); clear howto! Server Time
5 Nov 2024 11:17:22 EST (-0500)
  Using rand(); clear howto! (Message 1 to 8 of 8)  
From: Simon Lemieux
Subject: Using rand(); clear howto!
Date: 10 May 2001 22:33:15
Message: <3AFB17FE.C5519168@yahoo.com>
I'm currently trying to use rand to determine random location of objects in my
scene.  I would like rand to give the exact same result every time it runs.

I've been programming with povray for about more than a year now and I've made
some very nice stuff, whenever I needed random calculation or heavy math
algorythms, I would move to C++ and build a program that would output a
file.pov...

I don't want to do that anymore, I would like to use pov and pov only, I believe
there is a very common way to get a good random!

Here is an example of code that should normally give a cloud of dots, brighter
if there is more objects with antialiasing:

#declare n = 100 *2;
#declare i = 0;
#declare b = 3;
#while (i<n)

sphere { <rand(seed(i)), rand(seed(i+1)), 0>, 0.01 pigment { color rgb 1 }
finish { ambient 1 } translate <-0.5, -0.5, 0> scale 2}

#declare i = i+2;
#end

But this code give two Very well aligned lines (made of dots...), what is the
problem?

Thanks,
  Simon

-- 
+--------------------------------+---------------------------+
| Simon Lemieux                  | http://666Mhz.myip.org/   |
| Email : lem### [at] yahoocom | Povray and OpenGL Gallery |
+--------------------------------+---------------------------+


Post a reply to this message

From: Chris Huff
Subject: Re: Using rand(); clear howto!
Date: 10 May 2001 22:49:21
Message: <3afb5330@news.povray.org>
Simon Lemieux <lem### [at] yahoocom> wrote: 

> I'm currently trying to use rand to determine random location of
> objects in my scene.  I would like rand to give the exact same
> result every time it runs.

It will do this on its own, you have to go through special effort to
get it to work differently.


> Here is an example of code that should normally give a cloud of
> dots, brighter if there is more objects with antialiasing:

> #declare n = 100 *2;
> #declare i = 0;
> #declare b = 3;
> #while (i<n)

> sphere { <rand(seed(i)), rand(seed(i+1)), 0>, 0.01 pigment { color rgb 1 }
> finish { ambient 1 } translate <-0.5, -0.5, 0> scale 2}

> #declare i = i+2;
> #end

> But this code give two Very well aligned lines (made of dots...),
> what is the problem?

You are not only seeding the random number generator within the loop,
you are seeding it *for every call*. You need to create one stream and
use it.
(And some general advice, always use at least one capital letter in
variable names, to avoid possible conflict with future features...and
indent!)

#declare N = 100*2;
#declare I = 0;
#declare S = seed(54321);
#while (I<N)
    sphere { <rand(S), rand(S), 0>, 0.01
        pigment { color rgb 1 }
        finish { ambient 1 }
        translate <-0.5, -0.5, 0>
        scale 2
    }
    #declare I = I+2;
#end

And of course, this will only make random spheres in a plane, for a 3D
cloud you need to add a third rand(S) to the sphere location vector.

-- 
Christopher James Huff - chr### [at] maccom
Home Page: http://homepage.mac.com/chrishuff/
POV-Ray TAG e-mail: chr### [at] povrayorg
POV-Ray TAG web site: http://tag.povray.org/


Post a reply to this message

From: Simon Lemieux
Subject: Re: Using rand(); clear howto!
Date: 10 May 2001 23:39:05
Message: <3AFB276D.D0B36405@yahoo.com>
> You are not only seeding the random number generator within the loop,
> you are seeding it *for every call*. You need to create one stream and
> use it.

Hmmm, I see I got confused by the documentation that used different seed
streams...  This is very simple and I got it now!

> (And some general advice, always use at least one capital letter in
> variable names, to avoid possible conflict with future features...and
> indent!)

Good advice, I might not use it though, I'm used to 'i' and 'n' that way,
although I like nSegments, nPolygons, mVertex, etc...
As for indentation... that was just a sketch... ;)

Thanks a lot Chris!
	Simon

-- 
+--------------------------------+---------------------------+
| Simon Lemieux                  | http://666Mhz.myip.org/   |
| Email : lem### [at] yahoocom | Povray and OpenGL Gallery |
+--------------------------------+---------------------------+


Post a reply to this message

From: Warp
Subject: Re: Using rand(); clear howto!
Date: 11 May 2001 07:31:53
Message: <3afbcda9@news.povray.org>
Simon Lemieux <lem### [at] yahoocom> wrote:
: sphere { <rand(seed(i)), rand(seed(i+1)), 0>, 0.01 pigment { color rgb 1 }

  Do you make that in C++ as well? That is, something like:

for(int i=0; i<n; ++i)
{
  srand(i);
  int x = rand();
  srand(i+1);
  int y = rand();
}

  I suppose not. But that's what you are doing in the povray code.

-- 
#macro N(D,I)#if(I<6)cylinder{M()#local D[I]=div(D[I],104);M().5,2pigment{
rgb M()}}N(D,(D[I]>99?I:I+1))#end#end#macro M()<mod(D[I],13)-6,mod(div(D[I
],13),8)-3,10>#end blob{N(array[6]{11117333955,
7382340,3358,3900569407,970,4254934330},0)}//                     - Warp -


Post a reply to this message

From: Simon Lemieux
Subject: Re: Using rand(); clear howto!
Date: 11 May 2001 11:39:33
Message: <3AFBD050.CC80AF1D@yahoo.com>
>   Do you make that in C++ as well? That is, something like:
> 
> for(int i=0; i<n; ++i)
> {
>   srand(i);
>   int x = rand();
>   srand(i+1);
>   int y = rand();
> }
> 
>   I suppose not. But that's what you are doing in the povray code.

Yeah I know this looks extremely silly!  I was just confused from the manual
that used more than one seed at a time.. So I thought that you needed a new seed
for every rand()...  That's the reason why I prefered C++, but now that I
understand how it works, I realized I was just dumb!

Thanks,
  Simon

-- 
+--------------------------------+---------------------------+
| Simon Lemieux                  | http://666Mhz.myip.org/   |
| Email : lem### [at] yahoocom | Povray and OpenGL Gallery |
+--------------------------------+---------------------------+


Post a reply to this message

From: Warp
Subject: Re: Using rand(); clear howto!
Date: 11 May 2001 12:47:00
Message: <3afc1784@news.povray.org>
Simon Lemieux <lem### [at] yahoocom> wrote:
: That's the reason why I prefered C++, but now that I
: understand how it works, I realized I was just dumb!

  I think that most of us have made this kind of mistakes (or even worse)
many times with POV-Ray, so don't feel too dumb :)

  What I really like about POV-Ray's random numbers is the fact that you
can have several random number generators. This is usually quite difficult
with C++ or other programming languages (unless you make your own random
number implementation).
  For example, suppose that you have made a little code that positions
objects randomly in a loop and you search for an initial seed value which
gives a nice distribution of the objects.
  After this you get the idea that it would be nice if the objects were
also colored randomly.
  Oops! If you set the color of the objects randomly using the same generator
as is used to calculate the location of the objects, the locations will
be messed up and you will loose your nice distribution.
  But no worries: Just use a different random number generator (even with
the same initial seed value if you want) to color the objects. This way their
location will be unchanged.
  Nice.

-- 
#macro N(D,I)#if(I<6)cylinder{M()#local D[I]=div(D[I],104);M().5,2pigment{
rgb M()}}N(D,(D[I]>99?I:I+1))#end#end#macro M()<mod(D[I],13)-6,mod(div(D[I
],13),8)-3,10>#end blob{N(array[6]{11117333955,
7382340,3358,3900569407,970,4254934330},0)}//                     - Warp -


Post a reply to this message

From: Simon Lemieux
Subject: Re: Using rand(); clear howto!
Date: 11 May 2001 14:38:23
Message: <3AFBFA3B.330E9745@yahoo.com>
>   I think that most of us have made this kind of mistakes (or even worse)
> many times with POV-Ray, so don't feel too dumb :)
> 
>   What I really like about POV-Ray's random numbers is the fact that you
> can have several random number generators. This is usually quite difficult
> with C++ or other programming languages (unless you make your own random
> number implementation).
>   For example, suppose that you have made a little code that positions
> objects randomly in a loop and you search for an initial seed value which
> gives a nice distribution of the objects.
>   After this you get the idea that it would be nice if the objects were
> also colored randomly.
>   Oops! If you set the color of the objects randomly using the same generator
> as is used to calculate the location of the objects, the locations will
> be messed up and you will loose your nice distribution.
>   But no worries: Just use a different random number generator (even with
> the same initial seed value if you want) to color the objects. This way their
> location will be unchanged.
>   Nice.

Yeah, I see your point... It can be done too in C++, but this way is much more
easy...

-- 
+--------------------------------+---------------------------+
| Simon Lemieux                  | http://666Mhz.myip.org/   |
| Email : lem### [at] yahoocom | Povray and OpenGL Gallery |
+--------------------------------+---------------------------+


Post a reply to this message

From: Chris Huff
Subject: Re: Using rand(); clear howto!
Date: 11 May 2001 16:57:32
Message: <3afc522f@news.povray.org>
Simon Lemieux <lem### [at] yahoocom> wrote:

> Yeah, I see your point... It can be done too in C++, but this way is
> much more easy...

In C++ (and Objective-C), the first thing I did was write
random-number-generator classes to do things like gaussian
distributions, specialized ranges ([0,1], [-1,1], [a,b]), etc.


-- 
Christopher James Huff - chr### [at] maccom
Home Page: http://homepage.mac.com/chrishuff/
POV-Ray TAG e-mail: chr### [at] povrayorg
POV-Ray TAG web site: http://tag.povray.org/


Post a reply to this message

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