Randomly generate 0 ~ 9 dozen

Source: Internet
Author: User
Today, my friend asked me a question:
Requirement: generate 0 ~ 9. The number is 10 and cannot be repeated within 10 times.
I gave his answer without thinking about it at the time:
-----------------------------------------------------
# Include <iostream>
# Include <time. h>
Using namespace STD;
Int main ()
{
Srand (time (0 ));
Int n = rand () % 10;
For (INT I = 0; I <10; ++ I)
{
Printf ("% d", (n + I) % 10 );
}
Getchar ();
Return 0;
}
-----------------------------------------------------
However, he said, "You generate 0 ~ 9. The ten numbers are always ordered. "Yes, I generate 0 ~ 9 The total number of these 10 numbers is 0 ~ 9. He turned around in this circle and asked me if I could generate 0 in random order ~ 9. These 10 numbers are not repeated for 10 times. the answer I gave him was no. because I cannot guarantee that the numbers I have generated for 10 times are different, I can only add a certain number to avoid it. If anyone can, please help! Thank you!

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.