POV-Ray : Newsgroups : povray.newusers : trouble using random numbers Server Time
4 Sep 2024 16:18:17 EDT (-0400)
  trouble using random numbers (Message 1 to 7 of 7)  
From: Justin Smith
Subject: trouble using random numbers
Date: 4 Oct 2002 04:00:14
Message: <web.3d9d49943158db03e07c96340@news.povray.org>
I have this object that uses a whole bunch of random values, but it seems
that every time I run the scene, it looks exactly the same. Here's the
relevent code (since regular inequality signs won't work in web view here, I
am replacing them in this message with [ and ]):

#declare Conifer_Cluster =
union
{
  #declare numNeedles = 5 + rand(seed(12))*5;
  #declare iter = 0;
  #while(iter [ numNeedles)
    object { Conifer_Needle rotate
[rand(seed(iter))*80,0,rand(seed(iter))*80] }
    #declare iter = iter+1;
  #end
  rotate [-40,0,-40]
  rotate [90,0,0]
}

This is to generate a cluster of pine needles. The object referred to in the
code called "Conifer_Needle" is just a simple slender cylinder. Every time
I run this, there are exactly 5 needles created and they are always in the
same position. So I'm not getting some aspect of the random number
generation in POV-Ray... What do I need to do to get different results each
time? The reason it needs to be different is that later on I am going to
have another object that will refer to this object within a #while loop.


Post a reply to this message

From: Tim Nikias
Subject: Re: trouble using random numbers
Date: 4 Oct 2002 06:01:37
Message: <3d9d6701@news.povray.org>
The random-number generator of POV-Ray will always
yield the same row of numbers, no matter which
frame of animation your in. Just imagine you'd randomly
placed an object in a box, it would be jumping all
over the box for each new frame, something not very
desirable.

Also, what you're doing, is initializing the seed again
every time you require a new random number, and thus
get the first random number from those series.
You should rather use:

#declare Seed=seed(1);

and then "rand(Seed)" everywhere where you require a
random number.

In your case:

 #declare Conifer_Cluster =
 union
 {
   #declare numNeedles = 5 + rand(seed(12))*5;
   #declare iter = 0;
   #while(iter [ numNeedles)
#declare IterSeed=seed(iter);
object { Conifer_Needle rotate
 [rand(IterSeed)*80,0,rand(IterSeed)*80] }
     #declare iter = iter+1;
   #end
   rotate [-40,0,-40]
   rotate [90,0,0]
 }

Still, every new frame of an animation will use the
same random-numbers, you could only go about this
like this:

#declare IterSeed=seed(iter+clock*1000);

Look at the docs about rand / seed to find out more
about the pseudo-random-stream this function returns.

Regards, Tim

--
Tim Nikias
Homepage: http://www.digitaltwilight.de/no_lights/index.html
Email: Tim### [at] gmxde

> I have this object that uses a whole bunch of random values, but it seems
> that every time I run the scene, it looks exactly the same. Here's the
> relevent code (since regular inequality signs won't work in web view here,
I
> am replacing them in this message with [ and ]):
>
> #declare Conifer_Cluster =
> union
> {
>   #declare numNeedles = 5 + rand(seed(12))*5;
>   #declare iter = 0;
>   #while(iter [ numNeedles)
>     object { Conifer_Needle rotate
> [rand(seed(iter))*80,0,rand(seed(iter))*80] }
>     #declare iter = iter+1;
>   #end
>   rotate [-40,0,-40]
>   rotate [90,0,0]
> }
>
> This is to generate a cluster of pine needles. The object referred to in
the
> code called "Conifer_Needle" is just a simple slender cylinder. Every time
> I run this, there are exactly 5 needles created and they are always in the
> same position. So I'm not getting some aspect of the random number
> generation in POV-Ray... What do I need to do to get different results
each
> time? The reason it needs to be different is that later on I am going to
> have another object that will refer to this object within a #while loop.
>
>


Post a reply to this message

From: Warp
Subject: Re: trouble using random numbers
Date: 4 Oct 2002 07:34:03
Message: <3d9d7cab@news.povray.org>
Besides the fact that rand() always returns the same number sequence for
a given seed, you are using rand() in the wrong way.
  This is how rand() should be used:

#declare Conifer_Cluster =
  union
  {
    #declare Conifer_Seed = seed(0); // or whatever number you like
    #declare numNeedles = 5 + rand(Conifer_Seed)*5;
    #declare iter = 0;
    #while(iter < numNeedles)
      object
      { Conifer_Needle rotate<rand(Conifer_Seed)*80,0,rand(Conifer_Seed)*80> }
      #declare iter = iter+1;
    #end
    rotate <-40,0,-40>
    rotate <90,0,0>
  }


  Since there's no variable in POV-Ray which changes from render to render,
there's no way of getting a different seed from render to render.
  However, in an animation it's possible, as there are variables which
change. The most useful is 'frame_number'.
  Thus, in an animation you could change the Conifer_Seed declaration above
to look like this:

    #declare Conifer_Seed = seed(frame_number);

  This way you get a different distribution for each frame of the animation.

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


Post a reply to this message

From: Justin Smith
Subject: Re: trouble using random numbers
Date: 5 Oct 2002 00:05:02
Message: <web.3d9e6370c254ce755de492f30@news.povray.org>
Ah, I think I understand a little better now. Thanks. It is strange, though,
that there isn't a way to seed using the internal clock of the computer, as
most programming languages I've used are able to do.

Thanks for the help.


Post a reply to this message

From: Mike Williams
Subject: Re: trouble using random numbers
Date: 5 Oct 2002 03:00:37
Message: <+aUk7AAKyon9EwyG@econym.demon.co.uk>
Wasn't it Justin Smith who wrote:
>Ah, I think I understand a little better now. Thanks. It is strange, though,
>that there isn't a way to seed using the internal clock of the computer, as
>most programming languages I've used are able to do.

That's one of the MegaPOV features that didn't make it into POV 3.5.

MegaPOV has a variable called "tick_count" which returns the current
value of the CPU clock, so you could say "seed(tick_count)".

-- 
Mike Williams
Gentleman of Leisure


Post a reply to this message

From: Thorsten Froehlich
Subject: Re: trouble using random numbers
Date: 5 Oct 2002 03:59:04
Message: <3d9e9bc8@news.povray.org>
In article <web.3d9e6370c254ce755de492f30@news.povray.org> , "Justin Smith"
<t74### [at] yahoocom> wrote:

> Ah, I think I understand a little better now. Thanks. It is strange, though,
> that there isn't a way to seed using the internal clock of the computer, as
> most programming languages I've used are able to do.

It would be of no help to you if objects would be constantly moving every
time you render the scene.

    Thorsten

____________________________________________________
Thorsten Froehlich, Duisburg, Germany
e-mail: tho### [at] trfde

Visit POV-Ray on the web: http://mac.povray.org


Post a reply to this message

From: Justin Smith
Subject: Re: trouble using random numbers
Date: 5 Oct 2002 23:35:08
Message: <web.3d9faf3ac254ce755de492f30@news.povray.org>
Thorsten Froehlich wrote:
>In article <web.3d9e6370c254ce755de492f30[at]news.povray.org> , "Justin Smith"
><t74### [at] yahoocom> wrote:
>
>> Ah, I think I understand a little better now. Thanks. It is strange, though,
>> that there isn't a way to seed using the internal clock of the computer, as
>> most programming languages I've used are able to do.
>
>It would be of no help to you if objects would be constantly moving every
>time you render the scene.
>

Well, it would if they were minor background objects and I was trying to
generate random terrain each time, or something like that. If I wanted to
generate a random tree, and then generate another random tree in a
different scene, I wouldn't want them too look the same, and I'd rather not
have remember all the past seeds I've used on past trees. Well, its a minor
point, anyways.


Post a reply to this message

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