# Recipe 2.8 Making Numbers Even More Random

#### 2.8.1 Problem

You want to generate numbers that are more random than Perl's random numbers. Limitations of your C library's random number generator seeds can sometimes cause problems. The sequence of pseudo-random numbers may repeat too soon for some applications.

#### 2.8.2 Solution

Use a different random number generator, such as those provided by the Math::Random and Math::TrulyRandom modules from CPAN:

```use Math::TrulyRandom;
\$random = truly_random_value( );

use Math::Random;
\$random = random_uniform( );```

#### 2.8.3 Discussion

The Perl build process tries to find the best C-library routine to use for generating pseudo-random numbers, looking at rand(3), random(3), and drand48(3). (This can be changed manually at build time, however.) The standard library functions are getting pretty good, but some ancient implementations of the rand function return only 16-bit random numbers or have other algorithmic weaknesses, and may therefore not be sufficiently random for your purposes.

The Math::TrulyRandom module uses inadequacies of your system's timers to generate the random numbers. This takes a while, so it isn't useful for generating a lot of random numbers.

The Math::Random module uses the randlib library to generate random numbers. It also includes a wide range of related functions for generating random numbers according to specific distributions, such as binomial, poisson, and exponential.

The srand and rand functions in perlfunc(1) and Chapter 29 of Programming Perl; Recipe 2.6 and Recipe 2.7; the documentation for the CPAN modules Math::Random and Math::TrulyRandom  Chapter 1. Strings  Chapter 2. Numbers  Introduction  Recipe 2.1 Checking Whether a String Is a Valid Number  Recipe 2.2 Rounding Floating-Point Numbers  Recipe 2.3 Comparing Floating-Point Numbers  Recipe 2.4 Operating on a Series of Integers  Recipe 2.5 Working with Roman Numerals  Recipe 2.6 Generating Random Numbers  Recipe 2.7 Generating Repeatable Random Number Sequences  Recipe 2.8 Making Numbers Even More Random  Recipe 2.9 Generating Biased Random Numbers  Recipe 2.10 Doing Trigonometry in Degrees, Not Radians  Recipe 2.11 Calculating More Trigonometric Functions  Recipe 2.12 Taking Logarithms  Recipe 2.13 Multiplying Matrices  Recipe 2.14 Using Complex Numbers  Recipe 2.15 Converting Binary, Octal, and Hexadecimal Numbers  Recipe 2.16 Putting Commas in Numbers  Recipe 2.17 Printing Correct Plurals  Recipe 2.18 Program: Calculating Prime Factors  Chapter 3. Dates and Times  Chapter 4. Arrays  Chapter 5. Hashes  Chapter 6. Pattern Matching  Chapter 7. File Access  Chapter 8. File Contents  Chapter 9. Directories  Chapter 10. Subroutines  Chapter 11. References and Records  Chapter 12. Packages, Libraries, and Modules  Chapter 13. Classes, Objects, and Ties  Chapter 14. Database Access  Chapter 15. Interactivity  Chapter 16. Process Management and Communication  Chapter 17. Sockets  Chapter 18. Internet Services  Chapter 19. CGI Programming  Chapter 20. Web Automation  Chapter 21. mod_perl  Chapter 22. XML