PHP/Math/mt rand
Содержание
- 1 Finding a random line of a file
- 2 Generate Password()
- 3 Generate random floating-point values from 0 to 10 with two decimals
- 4 Generate random numbers
- 5 Get a random value from 5 to 25.
- 6 Get random values from �10 to 10.
- 7 int mt_rand ( [int min, int max] ) returns random numbers, similar to the rand( ).
- 8 Random floating-point values from 0 to 10 with two decimals
- 9 Random numbe with precision
- 10 Random values are not restricted to positive integers. The following example shows how to get random values from -10 to 10.
Finding a random line of a file
<source lang="html4strict">
<?php function randomint($max = 1) {
$m = 1000000; return ((mt_rand(1,$m * $max)-1)/$m);
} ?> $line_number = 0; $fh = fopen("data.txt","r") or die($php_errormsg); while (! feof($fh)) {
if ($s = fgets($fh)) { $line_number++; if (randomint($line_number) < 1) { $line = $s; } }
} fclose($fh) or die($php_errormsg); ?>
</source>
Generate Password()
<source lang="html4strict">
<?php function GeneratePassword($min = 5, $max = 8) {
$ValidChars = "abcdefghijklmnopqrstuvwxyz123456789"; $max_char = strlen($ValidChars) - 1; $length = mt_rand($min, $max); $password = ""; for ($i = 0; $i < $length; $i++) { $password .= $ValidChars[mt_rand(0, $max_char)]; } return $password;
} echo "New Password = " . GeneratePassword() . "\n"; echo "New Password = " . GeneratePassword(4, 10) . "\n"; ?>
</source>
Generate random floating-point values from 0 to 10 with two decimals
<source lang="html4strict">
<?php function frand($min, $max, $decimals = 0) {
$scale = pow(10, $decimals); return mt_rand($min * $scale, $max * $scale) / $scale;
} echo "frand(0, 10, 2) = " . frand(0, 10, 2) . "\n"; ?>
</source>
Generate random numbers
<source lang="html4strict">
<?php function frand($min, $max, $precision = 1) {
$scale = 1/$precision; return mt_rand($min * $scale, $max * $scale) / $scale;
} echo "frand(0, 10, 0.25) = " . frand(0, 10, 0.25) . "\n"; ?>
</source>
Get a random value from 5 to 25.
<source lang="html4strict">
<?php echo "rand(5, 25) = " . rand(5, 25) . "\n"; echo "mt_rand(5, 25) = " . mt_rand(5, 25) . "\n"; ?>
</source>
Get random values from �10 to 10.
<source lang="html4strict">
<?php echo "rand(-10, 10) = " . rand(-10, 10) . "\n"; echo "mt_rand(-10, 10) = " . mt_rand(-10, 10) . "\n"; ?>
</source>
int mt_rand ( [int min, int max] ) returns random numbers, similar to the rand( ).
<source lang="html4strict">
It uses the Mersenne Twister algorithm to generate "better" random numbers (i.e., more random), and is often preferred. <?
$mtrand = mt_rand( ); $mtrandrange = mt_rand(1,100);
?>
</source>
Random floating-point values from 0 to 10 with two decimals
<source lang="html4strict">
<?php function frand($min, $max, $decimals = 0) {
$scale = pow(10, $decimals); return mt_rand($min * $scale, $max * $scale) / $scale;
} echo "frand(0, 10, 2) = " . frand(0, 10, 2) . "\n"; ?>
</source>
Random numbe with precision
<source lang="html4strict">
<?php function frand($min, $max, $precision = 1) {
$scale = 1/$precision; return mt_rand($min * $scale, $max * $scale) / $scale;
} echo "frand(0, 10, 0.25) = " . frand(0, 10, 0.25) . "\n"; ?>
</source>
Random values are not restricted to positive integers. The following example shows how to get random values from -10 to 10.
<source lang="html4strict">
<?php echo "rand(-10, 10) = " . rand(-10, 10) . "\n"; echo "mt_rand(-10, 10) = " . mt_rand(-10, 10) . "\n"; ?>
</source>