繁体   English   中英

在 PHP 中生成类似于原始数字的 7 位数字

[英]Generate 7 digit number similar to original number in PHP

我想生成与 PHP 中的原始随机数非常相似的 7 位数字。

目的是:原始随机号码模拟声音游戏中使用的电话号码,用户在收听音频时识别正确号码。

使用循环

$original_randomized_number=rand(1000000,9999999);
for ($x = 1; $x <= 3; $x++) {
$generated_telephones[$x] = rand(1000000,9999999);
}

生成 3 个随机电话号码是可行的,但识别正确号码太简单了。

Fe 原始随机数 9876543 - 所需结果类似于 - 9865742、8896558、9586543

https://www.tehplayground.com/WJiQXDOWT5CR5SwS

$phone = "9876543";
function getSimilar($phone, $num) {
    $out = array();
    for( $x=0; $x<$num; $x++) {
    $new = array();
    $p = str_split($phone);
    $ctr=2;
    foreach ($p as $n) {
     $neg = rand(0,10) > 5? 1:-1;
     $n = abs((intVal($n) + ( rand(0,$ctr) * $neg)) );
     $new[] = min($n,9);
     $ctr++;
    }
    $out[]=implode("",$new); 
  }

  return $out;

}

$nphone = getSimilar($phone, 5);
echo $phone . "\n";
print_r( $nphone);

例如 output:

Array
(
    [0] => 9634999
    [1] => 7988194
    [2] => 9896724
    [3] => 8949969
    [4] => 8998434
)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM