
- By Sonya
Roberts
The next thing we need to look at is how to work with rand() when we want interger values instead of decimals.
As stated earlier, rand() returns a decimal value from 0 to 1. If we want to increase or decrease the value returned, we have to do some math on it. Ranges where we're happy with decimal values are easy; for example, to get a range of numbers from -5 to +5 we need just say -5+(rand(SD1)*10) and we'll get the desired set of values without any problems.
Suppose we want a set that doesn't include the value "0", such as a range from 1 to 5 inclusive. Again, as long as decimal values are okay, we can achieve this with only minor changes to our statement. This time, we would say 1+(rand(SD1)*4) to get the right range of values.
Now let's add in the idea of only wanting interger values, and wanting to have a statiscally equal chance of any of the intergers appearing. If we said int(1+(rand(SD1)*4)), what would our actual results be? For the numbers 1 through 4, they'd be fine..."1" would be any value from 1 to 1.999999 (for however many decimal places POV keeps track of), "2" would be 2 to 2.99999, etc. But "5"...the only value that would produce a "5" would be "5, i.e., that statistically rare chance when rand() returned a "1" exactly (1+1*4=5). What we need to do, in this case, is tack some extra decimals onto the end of our "4" to allow for the creation of decimal values of 5. We would need to say int(1+(rand(SD1)*4.99999)), and only then would the statistical chance of a "5" being returned approach equality with that of the other intergers.
Or another workaround for this would be to say int(1+(rand(SD1)*5)), but you would then need to ensure that your code knows what to do if that rare "6" actually does turn up.
Now that you have an understanding of how to correctly get interger values using the rand() function, let's add some more randomness to our scene; let's have POV make the scene out of random primitives instead of just spheres (we'll be using the #switch...#case...#break...#end directive for that), and also add some randomness to the finish of the objects
#declare SD1=seed(1)
#declare X1=1
object {
union {
#while (X1<=10)
#declare Z1=1
#while (Z1<=10)
#declare Shape=int(1+(rand(SD1)*4.99999))
#declare Look=
texture {
pigment {color rgb <rand(SD1),rand(SD1),rand(SD1)&gr;}
finish {
reflection rand(SD1)
ambient (rand(SD1)*.5)
phong rand(SD1)
}
}
#switch (Shape)
#case (1)
sphere {
<0,0,0>,.25
texture {Look}
translate
}
#break
#case (2)
box {
<-.25,-.25,-.25>,<.25,.25,.25>
texture {Look}
translate
}
#break
#case (3)
cylinder {
<0,-.25,0>,<0,.25,0>,.25
texture {Look}
translate
}
#break
#case (4)
torus {
.2,.05
texture {Look}
translate
}
#break
#case (5)
cone {
<0,-.25,0>,.25,<0,.25,0>,.1
texture {Look}
translate
}
#break
#end
#declare Z1=Z1+1
#end
#declare X1=X1+1
#end
}
}

As you can see, our image is getting quite interesting-looking; but it's still not a scene. But... imagine that instead of a grey-checked room, we had a ground plane and a sky sphere. Imagine, too, that instead of randomly positioning 5 randomly-selected primitives, we'd randomly positioned 5 randomly-different plants along that ground plane. Then you might have the beginnings of a scene:

In the next lesson, we will take a further look at the #while...#end and #switch...#case...#break...#end statements, and the rand() function, looking in closer detail at some of the specific effects you can achieve using them, including some common pitfalls and how to avoid or work around them.
The Rendering
Times: Design and Copyright © 1997 -- DCS
& WorkForce Graphics. All rights reserved.
Reproduction in whole or in part in any form or medium without
the express written permission of DCS and/or WorkForce Graphics
is prohibited.