The rand () function in C + + + produces the use of random numbers __jquery

Source: Internet
Author: User
Tags function prototype rand

I. Rand ()

1. Rand () does not require a parameter, it returns an arbitrary integer from 0 to the largest random number, and the maximum random number is usually a fixed large integer. That is, rand () returns a pseudo random number (integer) that ranges from 0 to Rand_max (32767).
2, the rand () function requires the header file is: "Stdlib.h"
RAND () function prototype: int rand (void);
Use the rand () function to generate a random integer within 0-99: int number = rand ()% 100;
3. Before invoking the rand () function, you can use the Srand () function to set the random number seed, and if no random number seed is set, the rand () function automatically designs the random number seed to be 1 when invoked. Random seeds are the same, and the random numbers are the same every time they are produced.
As follows: Produces a random number between 1 and 10, this example does not have random numbers of seeds

#include <stdio.h>
#include <stdlib.h>   
int main ()  
{  
    int i,j;  
    For (i=0 i<10; i++)  
    {  
        j=1+ (int) (10.0 * RAND ()/(rand_max+1.0));
        or  j = 1+ (int) (rand ()%10)  
        printf ("%d", j);  
    }  
Output 3 4 8 7 ten 2 4 8 9 6 3 4 8 7 10 2 4 8 9 6
//Again execution still produces the same random number

Second, Srand ()

1, the Srand () function requires the header file is still: "Stdlib.h"
Srand () function prototype: void Srand (usigned int seed);
2. Usually the random number generated by rand () is the same as the previous one at each run, so as to facilitate the debugging of the program. To produce a random number at a time, you can use the Srand (seed) function to produce a randomized seed, which can produce different random numbers, depending on the seed.
3, can also include "time.h" header file, the Srand (time (0)) is then used to randomize the random number generator, so that a different sequence of random numbers can be obtained every two times, which requires the program to run at intervals of two times more than 1 seconds.
As follows: Produces a random number between 1 and 10, this example and the execution result can be compared with rand ()

#include <time.h>
#include <stdio.h>   
#include <stdlib.h>   
int main ()  
{  
    int i,j ;  
    Srand ((int) time (0));  
    For (i=0 i<10; i++)  
    {  
        j = 1+ (int) (rand ()%10);  
        printf ("%d", j);  
    }  
}  
8 4 5 3 6 2 3 5 9 8 7 3 6 7 2 2-4 8 6
5//execute again to produce different random numbers

Methods of generating random integers within a specified range

1, using the method of "mode addition + addition"
in general, can be expressed as: int num = rand ()% n +a; The
where a is the starting value, the N-1+a is the terminating value, and N is the range of integers.
2, if the 1~100 is to be generated, this is the case: int num = rand ()% 100 + 1;
3, General: rand ()% (b-a+1) + A; represents a random integer between a~b.
(because rand ()% (b-a+1) represents an integer between 0~b-a)
4, to produce a decimal number between 0~1, you can obtain a 0~10 integer and then divide by 10 to get a "random to very bit" of 10 random decimals.
to get random decimal places with random to percentile, you need to get the 10 integers of the 0~100 first, then divide by 100, and so on.

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.