Summary of functions used in C language for generating random numbers _c language

Source: Internet
Author: User
Tags rand random seed

On the UNIX operating system and Windows operating system, we know that there is a function of Rand, which is used to generate random numbers of the function API interface, then how its principle to achieve?
If the contract a1=f (seed), An+1=f (an), then you can get a sequence a1,a2,a3 ... An, then to make a pseudo random function of Rand, you just have to let it return the next element of the sequence every time you call it. In fact, the equivalent of the 1th call to Rand return A1, the 2nd time to return to A2, ..., the nth time to return an, so that each return value is different, that is, the equivalent of random number. But not really random numbers, the real random numbers are generated by physical phenomena: such as throwing coins, dice, runners, noise using electronic components, nuclear fission, and so on. Such random number generators are called physical random number generators, their disadvantage is that the technical requirements are relatively high. So what exactly is a random number?

Random number: Random number is the number of randomly generated every time the code is run, the value of each generated number is indeterminate, returning a random integer value between 0 and Randmax, with no randmax value, and Randmax range at least between 32767 (int), that is, two bytes (16 digits). If you use the unsigned int double byte is 65535, four bytes is an integer range of 4294967295. and 0 to Randmax the probability of each number being selected is the same.

Principle: the principle of generating random numbers is based on a value, commonly referred to as random seed, and then the seed as a parameter, after a series of formula operations to produce a value, this value is random number.

The RAND function and the Srand function are used to use random numbers in the C language.

int rand (): The return value is a random value and the argument is null, and a random number is generated through the RAND function.

void Srand (unsigned int seed): The return value is null, which is to set the random seed, when we do not set the random seed, the default setting of the seed is 1, that is, Srand (1).

Use:

#include <stdlib.h>//to introduce stdlib.h this header file
int main ()
{
 int rand_num = rand ();
 printf ("Rand_num =%d\n", rand_num);
 return 0;
}

The results of each run are the same, why is this? As has been said, random numbers produce a random seed as a parameter, and then return a value, and the default random seed is 1, so the random number is the same every time it is generated.

If we modify the random seed, we will find that the random number is different from the original, but the result of each run is the same:

#include <stdlib.h>//to introduce stdlib.h this header file
int main ()
{
 srand (3);
 int rand_num = rand ();
 printf ("Rand_num =%d\n", rand_num);
 Srand (5);
 Rand_num = rand ();
 printf ("Rand_num =%d\n", rand_num);
 return 0;
}

Two times the output is different, my output is as follows:

Rand_num = 50421

rand_num = 847425747

But our program must be written well, do not change the random seeds, and then each produced a different value of Ah, then how do we do it? Since the random value produced is related to the seed, as long as the random seed is different each time, then the random value produced is not the same, we can take the time as a random seed, because each time the runtime is different, so the random value produced is not the same, so we can do this:

#include <time.h>//Use the time function must introduce the time.h header file
#include <stdlib.h>
int main ()
{
 srand (int) Time (0));
 int rand_num = rand ();
 printf ("Rand_num =%d\n", rand_num);
 return 0;
}

In this case, the output will be different each time.

Through the above method, we can get different random values, but we generally get a certain range of random values, such as return 0~100 between the return value, such as the simulation of the dice, random return 1~6 value. So how do we do that?

We're going to return the random value of the 0~6, just return the random value on top of the 7 to get the rest:

int rand_num = rand ()% 7;
printf ("Rand_num =%d\n", rand_num);

So if we want to return the random value of 0~a, as long as we can get a + 1, so we have the following formula:

int rand_num = rand ()% (a + 1);//Returns a random value of 0 ~ a

If we're going to return a random value of a ~ b, what is the formula? Because the random number can only return 0 to a certain number of random values, so a ~ B of the random value, we can first return 0 ~ (b–a) of the random value, and then add a can:

int rand_num = rand ()% (b-a + 1);//1, return 0 ~ (b-a) random value
Rand_num = Rand_num + A;//2, return a ~ b random value

So the formula for the combination of 1 and 2 above is:

int rand_num = rand ()% (b-a + 1) + a;//returns the random value of a ~ b

Now you do the following:

#include <stdlib.h>
int main ()
{
 srand (2);//random seed fixed to 2 for
 (int i = 0; i < 5; i++)
 {
 int rand_num = rand ();
 printf ("Rand_num =%d\n", rand_num);/note Output  
 } return
 0;
}

Since the random seed, why the output is not the same? It should be noted here that if the program does not end, and did not reset the random seed, then the system will be the last random value as the next random function of the random seed, so in the above for the loop, in fact, each cycle of the seeds are different, how to verify it? Look at me first. The output here is:

Rand_num = 33614

rand_num = 564950498

rand_num = 1097816499 rand_num =

1969887316 rand_num

= 140734213

We can set the random seed to one of the rand_num values, such as 33614, so if the output is 564950498, then the random value for the next random function is taken as a random seed in the for loop each time.

Srand (33614);
int rand_num = rand ();
printf ("Rand_num =%d\n", rand_num);

Results:

Rand_num = 564950498;

Verification completed.

Arc4random () function:

This function is a more intelligent random function in the C language package, we just call this function, we will produce random numbers, do not set random seeds, and the use is very simple:

int arc_rand = Arc4random ();
printf ("Arc_rand =%d\n", Arc_rand);

The results of each run are different. If you want to generate a ~ b random value, the formula is also:

Arc4random ()% (b-a + 1) + A;

Related Article

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.