C + + produces random numbers

Source: Internet
Author: User

Turn from: http://blog.csdn.net/yushuai007008/article/details/7038845

C + + How to generate random numbers: Here is the rand () function, the Srand () function, C + + does not have the random (int number) function.

(1) If you want to generate random numbers without setting a range, you can just use RAND (): rand () returns a random value ranging from 0 to Rand_max. The Rand_max value is at least 32767.
For example:
[CPP] view plain copy #include <stdio.h> #include <iostream> [CPP] view plain copy int   _tmain (int argc, _tchar* argv[]) {for (int i=0;i<10;i++) cout << rand () << Endl; }

(2) If you want to randomly generate a number in a certain range,

For example: randomly generated number of 10 0~99:
[CPP] view plain copy #include <stdio.h> #include <iostream> [CPP] view plain copy int _tmain (int argc, _tchar* argv[]) {for (int x=0;x<10;x++) cout << rand ()%100 << ""   ); }


In short, the random number that produces the A~b range is available: A+rand ()% (b-a+1)

(3) But the above two examples are still the same as the first when they are run multiple times. Such benefits are easy to debug, but lose the meaning of random numbers. If you want to have a different random number for each run, you will use the Srand () function. Srand () is used to set the random number seed for rand () to produce random numbers. Before you can invoke the rand () function to generate random numbers, you must first use Srand () to set up the random number seed (seed), and if no random number seed is set, rand () automatically sets the random number seed to 1 when it is called. The above two examples are because no random number seed is set, and each random number seed is automatically set to the same value of 1, which results in the same random values produced by Rand ().

Srand () function definition: void srand (unsigned int seed);
You can usually use the return value of Geypid () or time (0) as a seed
If you use Time (0), add the header file #include<time.h>

For example:
[CPP] view plain copy #include <stdio.h> #include <time.h> #inc Lude <iostream> int _tmain (int argc, _tchar* argv[]) {Srand ((unsigned) time (NULL));//srand ((unsigned) time      (0)) srand (int) time (0) is available for (int i=0;i<10;i++) cout << rand () << Endl; }

The results of each run will be different.

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.