Analysis of RAND function and Srand function in C language (i.)

Source: Internet
Author: User

We often need to use random numbers in the process of programming problem solving. Since the computer is a logic-based machine, it is impossible to be truly random (presumably a quantum computer can?). )。 So the computer generates pseudo-random numbers for us to use.

We use the RAND function in C to generate pseudo-random numbers.

A simple demonstration is as follows:

1#include <stdio.h>2#include <stdlib.h>3#include <time.h>4 5 int6MainintargcChar**argv)7 {8     //constructs a "seed" that generates a pseudo-random number at the current time of the machine. 9Srand ((unsignedint) Time (NULL));Ten     inti; One     //Print 10 pseudo-random numbers A      for(i =0; I <Ten; i++) { -          -printf"%d", Rand ()); the     } -printf"\ n"); -      -System"Pause"); +     return 0; -}

Obviously, if we do not use the Srand function of line nineth, then our program will print the same sequence of 10 pseudo-random numbers each time, on this machine is always 41, 18467, 6334, .... This is because the C language uses the linear congruential generator as the generator to generate pseudo-random numbers, but this generator generates pseudo-random numbers and requires a "seed" to perform the operation. And if we just call the RAND function, we always use the fixed "seed" set by the C language to generate pseudo-random numbers, so the sequence of generated pseudo-random numbers must be exactly the same.

When we use Srand to provide Rand with a different "seed" with time as a parameter, then each time the "seed" is different, the sequence of pseudo-random numbers is different, of course.

But from the implementation of the code, we cannot clearly see how the "seed" provided by the Srand function is used by the RAND function.

From the pseudo-random numbers section of the ISO C99 standard (ISO/IEC 9899:1999 (E)) (7.20.2 pseudo-random sequence generation functions), we can see a concise example of a portable implementation:

1 StaticUnsignedLong intNext =1;2 3 intRandvoid)//Rand_max assumed to be 327674 {5Next = next *1103515245+12345;6     return(unsignedint) (next/65536) %32768;7 }8 9 voidSrand (unsignedintseed)Ten { OneNext =seed; A}

In this example, the "seed" is the static internal variable next, with an initial value of 1. If we do not use Srand to update next, it is clear that the pseudo-random number generated by each of our invocations is the same (next starts from 1). If we use Srand in the program to update next, then each time we run the program, we give next a different value for the initialization, so we can get the sequence of the pseudo-random number.

But is the implementation of the RAND function and the Srand function really as simple as the example?

Put it in the next chapter and write it.

Analysis of RAND function and Srand function in C language (i.)

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.