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 resulting digital repetition rate very low, so it looks like a real random number, the program to achieve this function is called pseudo-random number generator.
There are many theories about how to generate random numbers, and if you want to discuss them in detail, the length of a book that needs to be thick. Regardless of the method used to implement the random number generator, it must be given an initial value named "Seed". And this value is best randomized, or at least pseudo-random. The value of "seed" is usually generated using a fast count register or shift register.
Let's talk about the use of the random number generator provided in the C language. The C compiler now provides a pseudo-random number generator function based on the ANSI standard, which is used to generate random numbers. They are the rand () and Srand () functions. These two functions work as follows:
1) First give Srand () a seed, it is a unsigned int type, its value range 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 () as many times as necessary to obtain a new random number without interruption;
4) Whenever possible, a new seed can be given to Srand () to further "Randomize" the output of rand ().

The problem is that if you provide the same seed value every time you call Srand (), you'll get the same sequence of random numbers, and you'll see that there are no random numbers, and the numbers are the same every time. For example, after calling Srand () with a seed value of 17, the first time you call rand (), you get a random number of 94. The second and third calls to Rand () will be given 26602 and 30017 respectively, which appear to be very random (although this is only a small collection of data points), but after you call Srand () again with 17 for the seed value, in the first three calls to Rand (), The resulting return value is still on the 94,26602,30017, and the resulting return value is still the rest of the return value obtained in the first call to Rand (). So only once again give Srand () a random seed value to get a random number again.

Random number and random number seed (RPM)

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.