|
Posted by Domestos on 04/23/06 04:15
People who work with computers often talk about their system's "random
number generator" and the "random numbers" it produces. However numbers
calculated by a computer through a deterministic process, cannot, by
definition, be random. Given knowledge of the algorithm used to create the
numbers and its internal state, you can predict all the numbers returned by
subsequent calls to the algorithm, whereas with genuinely random numbers,
knowledge of one number or an arbitrarily long sequence of numbers is of no
use whatsoever in predicting the next number to be generated.
Computer-generated "random" numbers are more properly referred to as
pseudo-random numbers, and pseudo-random sequences of such numbers. A
variety of clever algorithms have been developed which generate sequences of
numbers which pass every statistical test used to distinguish random
sequences from those containing some pattern or internal order.
PHP has a number of inbuilt random functions such as rand(). This function
must be first seeded by srand() in order for it to work properly.
<?php
// Range of numbers
// Minimum number
$min = "1";
// Maximum number
$max = "10";
srand((double) microtime() * 1000003);
// 1000003 is a prime number
echo "rand() pseudo-random number".
rand($min, $max);
?>
Navigation:
[Reply to this message]
|