Last active
August 29, 2015 14:14
-
-
Save jntme/6677d6e76278e1fb2f3e to your computer and use it in GitHub Desktop.
is rand() in php really random?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.