How do new users generate non-repeated 4-digit numbers?

Source: Internet
Author: User
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;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.