POV-Ray Raytracer Descriptions and Examples by Friedrich A. Lohmüller
   Random with POV-Ray (Part 1)
Italiano Italiano
Français français
Deutsch Deutsch

Home
- POV-Ray Tutorial

Random in POV-Ray
  > RandomNumbers in Loops
   - quadratic + cubic
   - Colors + Scale
   - Tilting + Bending
   - Trees by Random
   - Include File 'rand.inc'
   - Filled height_field
   - Random Flower

  - Insert Menu Add-on
    & Download
 
                                       
 Basics on
Random Numbers and Loops
     

POV-Ray has a built-in function to generate different sequences of random numbers. By using this random number function in while loops we are able to simulate with POV-Ray very realistic and natural looking distributions.
More precisely this function generates "peudo random numbers", this means with each new rendering we get the same serie of randomly distributed numbers - a reproducible kind of random.
To demonstrate the effect of this random function we use here while loops to arrange objects regularly. Basics about the use of while loops with POV-Ray are explained in the section "While Loops with POV-Ray".

Hint: All POV-Ray scene files of the following samples are downloadable (with the extension: .pov, alternativly also as .txt text files!).

 
First we use a simple while loop to arrange spheres along the x axis from x = -5 to x = +5:

//---------------------------------
#declare Ball =
sphere{<0,0,0>,0.25
       texture{
        pigment{color rgb<1,0.65,0>}
        finish {phong 1}
              } // end of texture
       } // end of sphere ----------
#declare NrX = -10;     // start
#declare EndNrX = 10;   // end
#while (NrX < EndNrX+1)
 object{Ball translate<NrX*0.5,0,0>}

 #declare NrX = NrX + 1;  //next Nr
#end // ----------- end of loop ----

 
 

With "#declare Random_1 = seed (1153);" we define the random number stream to use.
We call these values by "rand( Random_1 )". Each call ot this kind generates another random number uniformly distributed with a value between 0 and 1.

//----------------------------------
#declare Rnd_1 = seed (1153);
//----------------------------------
#declare Ball =
sphere{<0,0,0>,0.5
       texture{
        pigment{color rgb<1,0.65,0>}
        finish {phong 1}
       } // end of texture
      } // end of sphere ----------
#declare NrX = -5;     // start
#declare EndNrX = 5;   // end
#while (NrX < EndNrX+1)
object{Ball
       translate<NrX,rand(Rnd_1),0>}

 #declare NrX = NrX + 1;  //next Nr
#end // ----------- end of loop ----

In the definition of the random number stream by "#declare Random_1 = seed (1153);" every other "seed" number generates a different stream of random nubers. Because of this we can try out very easily other strems with different "seed" numbers - may be other seed numbers produce a "nicer" sequence of random numbers.
Note: This is a reproduceable random, this means: With each new rendering of the same scene file we are getting just the same sequence of values between 0 and 1. Random numbers which are produced in this way are strictly speaking better called pseudo-random numbers.
Some examples for the use of 'rand(...)':
values between 0 and 1:       " rand( Random_1) "
values between 0 and 5:       " 5*rand( Random_1) "
values between -1 and +1:    " -1+2* rand( Random_1) " or 2*(0.5-rand( Random_1)) "
integers between 0 and 10 :  "  int( 10*rand( Random_1) )  "


//----------------------------------
#declare Rnd_1 = seed (1153);
#declare Rnd_2 = seed (553) ;
//----------------------------------
#declare Ball_1 =
sphere{<0,0,0>,0.5
       texture{
        pigment{color rgb<1,0.65,0>}
        finish {phong 1}
              } // end of texture
       } // end of sphere ----------
#declare Ball_2 =
sphere{<0,0,0>,0.5
       texture{
        pigment{color <1,0.2.0,0>}
        finish {phong 1}
              } // end of texture
       } // end of sphere ----------
#declare NrX = -5;     // start
#declare EndNrX = 5;   // end
#while (NrX < EndNrX+1)
object{Ball_1
    translate<NrX,5*rand(Rnd_1),0>}
object{Ball_2
    translate<NrX,2*rand(Rnd_2),0>}

 #declare NrX = NrX + 1;  //next Nr
#end // ---------- end of loop -----






0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8
top

© Friedrich A. Lohmüller, 2015
www.f-lohmueller.de
 
Visitors:
(since May-1999)
Thanks for your visit!