I have a function called rndgen() generate random 6 digits number that can have zero digit everywhere even beginning of number.
example:
267809
671485
200000
037481
000005
000437
500005
777777
I dont want to generate numbers like 000024 or 240000 or 000005 or 140000 or 500005 or 999999, and there are numbers like 000124 or 245000 or 000555 or 145000 or 511005 or 999887 allowed to generate.
All numbers that have the same digits are not allowed
How can I check this number if more than three digits are the beginning or end of the zero OR more than three zero in the middle of the number, it generate a new number, but if the zeros were at the beginning or end, lower or none could use the same number( BY PHP )?
The Rules I want:
after generating number>
IF ( NUMBER HAS MORE THAN THREE ZERO AT THE BEGINNING OR END OF NUMBER
OR
WHOLE NUMBER HAVE THE SAME DIGITS
OR
NUMBER HAS MORE THAN THREE SAME DIGITS AT THE BEGINNING OR END OF NUMBER
OR
NUMBER HAS MORE THAN THREE ZERO AT IN THE MIDDLE OF NUMBER
)
{ CALL AGAIN rndgen() TO GENERATE NEW RANDOM 6 DIGITS NUMBER }
thanks
Using arithmetic:
function rndgen() {
do {
$n = mt_rand(100, 999899);
} while ( !($n/100%1111 && $n%1e4%1111 && $n%1e5 > 9) );
return sprintf("%06d", $n);
}
echo rndgen();
About the number range 100, 999899: all numbers lower than 100 need to be excluded since they all returns 4 leading zeros: 0000xx, and all numbers greater than 999899 need to be excluded too since they all have 4 leading nine: 9999xx
About the conditions:
$n/100%1111
: shifts the dozens and units to the decimal part and calculates the 1111 modulo. ex:
$n 123456 444412 124444 100002
$n/100 1234.56 4444.12 1244.44 1000.02
%1111 123 0 133 1000
$n%1e4%1111
: removes dozens of thousands and calculates the 1111 modulo. ex:
$n 123456 444412 124444 100002
$n%1e4 3456 4412 4444 2
%1111 123 1079 0 2
$n%1e5 > 9
: calculates the 100000 modulo and checks if it is greater than 9. ex:
$n 123456 444412 124444 100002
$n%1e5 23456 44412 4444 2
> 9 true true true false
It seems you want to reject numbers that have:
This you can do with the following regular expression in a preg_match
function call:
^(\d)\1\1\1|(\d)\2\2\2$|0000
If that matches, then you have a number that should be rejected.
Here is code to test several numbers:
$tests = array(
"000024", "241111", "222225", "143333", "500005", "999999",
"000124", "245111", "222555", "145333", "544445", "799997"
);
foreach($tests as $num) {
$reject = preg_match("~^(\d)\\1\\1\\1|(\d)\\2\\2\\2$|0000~", $num);
echo "$num: " . ($reject ? "Not OK" : "OK") . "\n";
}
The first 6 will print as "Not OK", the other 6 as "OK".
Your rndgen
function could use that as follows:
function rndgen() {
do {
$num = sprintf('%06d', mt_rand(100, 999989));
} while (preg_match("~^(\d)\\1\\1\\1|(\d)\\2\\2\\2$|0000~", $num));
return $num;
}
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.