Random Number Generation Algorithm in C Language

Source: Internet
Author: User

Random Number Generation Algorithm in C Language

Random number, that is, different values are generated at different times. On UNIX and Windows operating systems, we know that there is a function rand, which is a function API interface used to generate random numbers. How does it work?

If a1 = f (seed) and an + 1 = f (an) are agreed, an a1, a2, a3 .. to create a pseudo-random function rand, you only need to make it return the next element of the sequence every time it is called. In fact, it is equivalent to 1st calls to rand to return a1, 2nd returns a2 ,..., Returns an for the nth time, so that the values returned each time are different, that is, they are equivalent to random numbers. But it is not really a random number. Real random numbers are produced by physical phenomena, such as coin throwing, 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 they have high technical requirements.

In the current computer field, many random number algorithms are generated based on this principle. Generally, when we use the rand function, we need a bell, that is, a random number seed, make sure that the values generated each time are different. At this time, we need a srand () function, and then in a certain period of time () function to generate different sequences.

This is generally the case when writing: srand (time (NULL); this is equivalent to a seed. Next let's take a look at the specific code:

 

# Include
 
  
# Include
  
   
Static unsigned int randseed;/* Support for pseudo-random numbers */unsigned int Curl_rand (void) {unsigned int r;/* return a pseudo-random number with an unsigned 32-bit integer. */r = randseed * 1103515245 + 12345; return (r <16) | (r> 16) & 0 xFFFF);} void Curl_srand (void) {/* generates a random pseudo-random number sequence. */Randseed = (unsigned int) time (NULL); Curl_rand ();} int main (void) {srand (); unsigned int I; int j = 10; printf ("generate 10 random numbers: \ n"); while (j! = 0) {I = Curl_rand () % 100; printf ("I: % d \ n", I); j --;} return 0 ;}
  
 
Execution result:


If such an algorithm is not used, the interface implemented by the system is called as follows:

# Include
 
  
# Include
  
   
# Include
   
    
Int main (void) {// seed srand (time (NULL); int I; int j; for (I = 0; I <10; I ++) {// generate 10 random numbers less than 100 j = rand () % 100; printf ("j: % d \ n", j);} return 0 ;}
   
  
 
Running result:

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.