Skip to content

Instantly share code, notes, and snippets.

@jntme
Last active August 29, 2015 14:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jntme/6677d6e76278e1fb2f3e to your computer and use it in GitHub Desktop.
Save jntme/6677d6e76278e1fb2f3e to your computer and use it in GitHub Desktop.
is rand() in php really random?
<?php
function countValueInArray($value, $array) {
$count = 0;
for ($i=0; $i < count($array); $i++) {
if($value == $array[$i]) {
$count++;
}
}
return $count;
}
$numbers = array();
for ($i=0; $i < 100; $i++) {
$numbers[$i] = rand(0, 100);
}
$duplicates = array();
for ($x=0; $x < count($numbers); $x++) {
$number = countValueInArray($numbers[$x], $numbers);
if ($number > 1) {
array_push($duplicates, $numbers[$x]);
}
}
$duplicatesList = array_values(array_unique($duplicates));
echo "number of duplicates: ".count($duplicatesList);
echo "<br>these are: <br>";
print_r($duplicatesList);
?>
@jntme
Copy link
Author

jntme commented Jan 30, 2015

I wanted to find out if the rand()-function is really random.
This script creates 100 random numbers in the range between 0 and 100. After that it counts if a number appears more than once in the array.
The result is a count of the duplicate numbers as the list of them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment