I would like to generate 7 digit number that is very similar to the original randomized number in PHP.
The purpose is: the original randomized number simulates the telephone number that is used in a sound game where users identifies the correct number while listening to the audio.
Using loop
$original_randomized_number=rand(1000000,9999999);
for ($x = 1; $x <= 3; $x++) {
$generated_telephones[$x] = rand(1000000,9999999);
}
to generate 3 random telephone numbers works, but it is too simple to identify the correct number.
Fe the original randomized number 9876543 - the desired result similar to - 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);
example output:
Array
(
[0] => 9634999
[1] => 7988194
[2] => 9896724
[3] => 8949969
[4] => 8998434
)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.