How C + + generates random numbers

Source: Internet
Author: User

First, use the rand () function

Header Files <stdlib.h>

(1) If you want to generate random numbers without setting the range, you can just use RAND (): rand () returns a random number, ranging from 0 to Rand_max. Rand_max is defined in stdlib.h with a value of 2147483647.


For example:

#include <stdio.h> #include <stdlib.h>void main () {       for (int i=0;i<10;i+)             printf ("%d/n", Rand () );}

(2) If you want to randomly generate a number in a range, you can define a random (int number) function in the macro definition, and then call the random () function directly inside the main ()

x = rand ()%11; /* Generates a random integer between 1~10 */

y = rand ()%51-25; /* Generate a random integer between 25 ~ 25 */

z = (double) rand ()/rand_max) * (B-A) + a;/* generation interval [b] random number */

#include <iostream> #include <stdlib.h>using namespace std; #define RANDOM (X) (rand ()%x) void Main () {for (int i = 0; I < 10; i++) {cout << random () << "";} cout << endl;system ("pause");}

(3) But the random number generated by the above two examples can only be a one-time, if you run the second time the output will be the same as the first 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 two examples above are that because no random number seed is set, 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 seed, and if you use time (0), add a header file #include<time.h>

#include <iostream> #include <stdlib.h> #include <time.h>using namespace std; #define RANDOM (X) (rand ( %x) void Main () {srand ((int) time (0)), for (int i = 0; i <; i++) {cout << random () << "";} cout << endl;system ("pause");}
(4) Random number in the interval (a, b)
#include <iostream> #include <stdlib.h> #include <time.h> #include <iomanip>using namespace std ; #define Random (A, B) (((double) rand ()/rand_max) * (b-a) +a) void Main () {srand ((int) time (0)); for (int i = 0; i <; i++) {cout << random (0, ten) << "";} cout << endl;system ("pause");}
(5) Rounding, returning integers, implemented by floor and Ceil functions
#include <iostream> #include <stdlib.h> #include <time.h> #include <iomanip>using namespace std ;/* Rounding, returning integer */double round (double R) {return (R > 0.0) Floor (R + 0.5): Ceil (r-0.5);} Double Randomrange (double A, double b) {Double x = (double) rand ()/rand_max;double result = x* (b-a) + A;return round (res ULT);}  void Main () {srand ((int) time (0)), for (int i = 0; i <; i++) {//cout << fixed << setprecision (0) << Randomrange (0) << ""; cout << randomrange (0, ten) << "";} cout << endl;system ("pause");}

How C + + generates random numbers

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.