Next: Declaring variables
Up: Variables
Previous: Hashes
Contents
Index
Random values
As an aside, often in testing out a program you don't
care about particular values for a variable. If you
want to just use random values, consider using the
rand function:
- $r = rand; sets $r to a random number between 0 and 1;
- $r = rand 10; sets $r to a random number between 0 and
up to, but not including, 10;
- $r = int(rand 10); sets $r a random integer between
0 and 9;
A handy snippet to stick in the back of your mind is that
$array[rand @array]; will return a random element
from an array @array - rand @array interprets
@array to be the size of the array, takes a random
number between 0 and the array size, and then automatically calls
int on the result, since it only makes sense to have
integer array indices.