C language generates random numbers

Source: Internet
Author: User

C language/c++ generates random numbers: The function to be used is rand (), Srand (), and Time ()

It should be stated that the iostream header file has the definition of the Srand function, no additional stdlib.h is required, and the use of the time () function requires the introduction of the CTime header file.


Use the rand () function to get a random number

If you just generate a random number without setting a range, you can just use RAND (): rand () returns a random value between 0 and Rand_max. Rand_max is defined in stdlib.h with a value of 2147483647.

  1.     for int  i=0;i<10;i++)
  2. Cout<<rand () <<endl;


Use the rand () function to get a random number within a certain range

If you want to get the number within a certain range, do the corresponding division to take more than.

     for(int i=0;i<10;i++)  
    1.         cout<<rand ()%10<<endl;   &NBSP; //produces an integer   within 10; &NBSP;&NBSP;

All we get is random integers, so how do we get decimals ?

For example: We can get an integer within 10001 (0~10000), and then divide the integer by 10000 to get the decimal of two digits after the decimal point.

  1.     for int  i=0;i<10;i++)
  2. cout<< (rand ()%10001)/10000.0<<endl;
Note that after 10000.0 there is a decimal point, which indicates that the result is a floating-point number.



Using the RAND function and the time function

With multiple runs, the program generates 10 random numbers, but the 10 random numbers are fixed, meaning they do not change over time.

This is related to the Srand () function. The Srand () is used to set the random number seed when rand () produces a random number. Before calling the rand () function to produce a random number, the random number seed (seed) must be set with Srand (), and if no random number seed is set, rand () automatically sets the random number seed to 1 when called.

The above example is 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 generated by rand ().

Srand () function definition: void srand (unsigned int seed);

You can usually use the return value of Geypid () or time (0) as the seed

If you use Time (0), you want to add the header file #include<ctime> times (0) or when (NULL) to return the system (from Midnight 1970.1.1), unit: Seconds

  1. Srand (Time (0));
  2.     for int  i=0;i<10;i++)  
  3.         //produces an integer   within 10; &NBSP;&NBSP;
  4. Cout<<rand ()%10<<endl;

If you do this, the results will be different for each run.


Precautions

    • The number of random numbers within a certain range.

To take a random integer between [0,10], the return value of rand () will be modeled with 10.

Randnumber = rand() % ;

So, what if the value is not starting from 0? You just need to remember a common formula.

To take a random integer (including a, but not including b) between [A and a], use:

(rand ()% (b-a)) + a

    • Pseudo-random floating-point number.

To obtain a floating-point number between 0~1, you can use:

rand ()/(double) (Rand_max)

If you want to take a larger range of random floating-point numbers, such as 0~100, you can use the following methods:

rand ()/((double) (Rand_max)/100)



from:http://blog.csdn.net/pipisorry/article/details/38661749

Ref:http://www.cppblog.com/xiaozhixd/archive/2010/05/05/114500.html


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.