home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam

## 2.9. Generating Biased Random Numbers

### 2.9.3. Discussion

The gaussian_rand function implements the polar Box Muller method for turning two independent, uniformly distributed random numbers between 0 and 1 (such as rand returns) into two numbers with a mean of 0 and a standard deviation of 1 (i.e., a Gaussian distribution). To generate numbers with a different mean and standard deviation, multiply the output of gaussian_rand by the new standard deviation, and then add the new mean:

```# gaussian_rand as shown earlier
\$mean = 25;
\$sdev = 2;
\$salary = gaussian_rand( ) * \$sdev + \$mean;
printf("You have been hired at \\$%.2f\n", \$salary);```

The Math::Random module implements this and other distributions for you:

```use Math::Random qw(random_normal);
\$salary = random_normal(1, \$mean, \$sdev);```

The weighted_rand function picks a random number between 0 and 1. It then uses the probabilities generated by weight_to_dist to see which element the random number corresponds to. Because of the vagaries of floating-point representation, accumulated errors in representation might mean we don't find an element to return. This is why we wrap the code in a while to pick a new random number and try again.

Also, the CPAN module Math::Random has functions to return random numbers from a variety of distributions.