<form method="post">
<input type="submit" name="submit">
</form>
<?php
if(isset($_POST['submit'])){
$random = random_int(0,999999999999999);
header('Location: ?r='.$random.'');
}
class Random {
private static $RSeed = 0;
public static function seed($s = 0) {
self::$RSeed = abs(intval($s)) % 9999999 + 1;
self::num();
}
public static function num($min = 0, $max = 9999999) {
if (self::$RSeed == 0) self::seed(mt_rand());
self::$RSeed = (self::$RSeed * 125) % 2796203;
return self::$RSeed % ($max - $min + 1) + $min;
}
}
$double[] = array();
$return ="";
if(isset($_GET["r"])){
Random::seed($_GET["r"]);
$x= 0;
while($x < 50){
$random = Random::num(1, 1200) . '<br />';
foreach($double as $val){
if($val == $random){
$return = "true";
}
}
if(!($return == "true")){
echo $random;
$double[] = $random;
$x++;
}
}
}else{
$random = random_int(0,999999999999999);
header('Location: ?r='.$random.'');
}
?>
答案 0 :(得分:0)
如果您想要一个随机且不重复的序列,请尝试使用范围和随机播放功能
$random_sequence = range($min, $max);
mt_srand($seed); # Use srand in php 5
shuffle($random_sequence);
答案 1 :(得分:0)
通过种子生成数字的方法有很多。
这是我基于Lehmer算法通过种子获得32位随机数的方法。这对于真正的过程生成非常有用。
获取种子为1000的随机数
$random = lgc_rand(1000);
576869358
使用取模强制范围为0-256
$random = lgc_rand(1000) % 256;
238
这是您可以调用的函数:
function lcg_rand(int $seed = 1): int {
return ($seed + 1) * 279470273 % 0xfffffffb;
}