Random Number and random number Seed

Source: Internet
Author: User
Tags random seed
There is no real random number generator in the computer, but it can make the generated digital repetition rate very low, it seems like a real random number, to achieve this function ProgramIt is called a pseudo-random number generator.
There are many theories about how to generate random numbers. To discuss them in detail, we need a thick book. No matter what method is used to implement the random number generator, you must provide it with an initial value named "Seed ".. And the value should be random, or at least the value is pseudo-random.. The value of the "seed" is usually generated using the fast count register or shift register.
The following describes the usage of the random number generator provided in the C language. Currently, the C compiler provides an ANSI-based pseudo-random number generator function to generate random numbers. They are the rand () and srand () functions. The two functions work as follows:
1) First, provide a seed for srand (). It is of the unsigned int type and its value ranges from 0 ~ 65535;
2) then call Rand (), which returns a random number (between 0 and 32767) based on the seed value provided to srand)
3) call Rand () multiple times as needed to continuously obtain new random numbers;
4) at any time, srand () can be provided with a new seed to further "randomize" the output results of rand.

This process looks very simple. The problem is that if you provide the same seed value every time you call srand (), you will get the same random number sequence. In this case, we can see that there is no random number, each time the number is the same. For example, after srand () is called as the seed value of 17, the random number 94 is obtained when rand () is called for the first time. When rand () is called for the second and third times, 26602 and 30017 are obtained respectively. These numbers seem very random (although this is just a small set of data points, after you call srand () with 17 as the seed value again, in the first three calls to Rand (), the returned values are still 30017, in addition, the subsequent returned values are the remaining returned values in the first call to Rand. Therefore, a random number can be obtained only when srand () is given a random seed value.

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.