Exploring the secrets of Rand

Source: Internet
Author: User

This long-name postA random function generated by Rand increases progressively.And the teacher said, "Because the time of two adjacent loops is less than 1 second, the seed value is the same, so the random number is the same ", it aroused my interest in rand. Just a few days ago, I saw some implementations of the C programming language:

 
Unsigned long int next = 1; // rand function, the returned value is 0 ~ A pseudo-random number between 32767. Int rand (void) {next = next * 1103515245 + 12345; Return (unsigned INT) (Next/65536) % 32768;} // srand function, Rand () the function sets the seed value void srand (unsigned int seed) {next = seed ;}

Then I answered it myself. I'm going to take a look at today's new lesson 10. I had no choice but to sleep for a day. Before going to bed, I tested whether the above definition was actually implemented:

 
# Include <stdio. h> # include <stdlib. h> // custom Rand srand function unsigned long int next _ = 1; int rand_test (void) {next _ = next _ * 1103515245 + 12345; Return (unsigned INT) (next _/65536) % 32768;} void srand_test (unsigned int seed) {next _ = seed;} int main () {// first output the case where no seed value is used. Printf ("% d-", rand_test (); printf ("% d \ n", Rand (); int I = 0; while (I ++ <20) {srand_test (I); printf ("% d-", rand_test (); srand (I ); printf ("% d \ n", Rand ();} return 0 ;}


Oh, no.

And intelligently right-click rand () to implement, Emma, and turn to the stdlib. h header file, which is displayed by a maximum of interfaces. No.

Search and search.


Source code of the Rand and srand standard library. I don't know much about it. Besides, there are also emerging structural members.

We can see that srand () is only assigned a value.

What about rand ()? The _ getptd () function does not care about it. The following returns such a length. Let's figure it out.

 
# Include <stdio. h> # include <stdlib. h> // custom Rand srand function unsigned long int next _ = 1; void srand_test (unsigned int seed) {next _ = seed;} int rand_test (void) {return next _ = (next _ * 214013l + 2531011l)> 16) & 0x7fff);} int main () {// first output the case where no seed value is used. Printf ("% d-", rand_test (); printf ("% d \ n", Rand (); int I = 0; while (I ++ <20) {srand_test (I); printf ("% d-", rand_test (); srand (I ); printf ("% d \ n", Rand ();} return 0 ;}



 
This is how you move forward. Even if you have been given something, this is how you move forward. Even if you have been taken away, this is how you move forward. Even if you miss something, this is how you move forward.
Happy Song ~

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.