How do new users seek help to generate non-repeated 4-digit numbers ;? Php // then generate the four-digit functionsc () {& nbsp; for ($ I = 0; $ I & lt; 4; $ I ++) {$ ygsz = rand (0, 9); $ sgesz. = $ ygsz;} & nbsp; for ($ a = 0; How do new users seek help to generate four numbers that are not repeated?
// Generate a four-digit number.
Function SC (){
For ($ I = 0; $ I <4; $ I ++ ){
$ Ygsz = rand (0, 9 );
$ Sgesz. = $ ygsz;
}
For ($ a = 0; $ a <3; $ a ++ ){
If ($ sgesz [$ a] ==$ sgesz [$ a + 1]) {
$ Vv = SC ();
}
}
Return $ sgesz;
}
$ Zqsz = SC ();
?>
How to change it?
------ Solution --------------------
PHP code
Function SC () {$ ar = array (); for ($ I = 0; $ I <4; $ I ++) {$ r = rand (0, 9 ); while (in_array ($ r, $ ar) $ r = rand (0, 9); array_push ($ ar, $ r);} return implode ('', $ ar) ;}echo SC ();
------ Solution --------------------
PHP code
$ Arr = Array ('0', '1', '2', '3', '4', '5', '6', '7 ', '8', '9'); // define the array shuffle ($ arr); // disrupt the element order $ rand = array_slice ($ arr ); // obtain the first four elements $ result = implode ('', $ rand); // convert it into a string echo $ result;