Rand Random Number Generation

Source: Internet
Author: User

Rand ()

Used to generate random numbers. Strictly speaking, the pseudo-random number is generated ). In a program, two calls to Rand () are used to generate random numbers and the results are the same.

For (INT I = 0; I <size; I ++)

{

Int x = rand () % 20;

Cout <x <Endl;

}

Used to obtain a random number of less than 20. If you use it multiple times, you can only get the same random array.

If you want to obtain two different random numbers, you can obtain them at different system times:

Srand (time (0 ));

The header file "time. H" should be included. Note that the interval between two calls should be more than one second to obtain different random arrays.

Srand (time (0 ));

For (INT I = 0; I <size; I ++)

{

Int x = rand () % 20;

Cout <x <Endl;

}

If the interval between two calls is less than 10 seconds, you can use sleep. In Windows, sleep is measured in milliseconds. in Linux, sleep is measured in seconds. In standard C, S is not capitalized. In VC, s must be capitalized. (Pay attention to it here)


Here we only talk about how to use rand () random time. We need to check the relevant information about the theory.


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.