C language based on the use of random functions

  • 2020-04-01 23:27:33
  • OfStack

In C, rand () function can be used to generate a random number, but it's not really in the sense of random Numbers, is a pseudo-random number, is based on a number, we can call it seeds, as the benchmark estimates a recurrence formula of the coefficient, when the large number of series, conform to the normal release, which is equivalent to generate a random number, but this is not the real random number, when the normal boot up the computer, the seed value is settled, unless you destroy the system, in order to change the value of the seed, C provides srand () function, Its original form is void srand(int a).
Maybe everyone knows the random function in C language, but the random function is not ANSI C standard, so it cannot be compiled in GCC,vc and other compilers.

Rand () returns a random number between 0 and RAND_MAX. Returns a random number between 0 and RAND_MAX, which is defined in stdlib.h (the value is at least 32767). The result of my operation is an indeterminate number, depending on the type of variable you defined, the int int is 32767. Before calling this function to generate a random number, a random number seed must be set using srand(). If no random number seed is set, rand() automatically sets the random number seed to 1 when it is called. The for statement is used to set the number of seeds. See the following example for details.

How do you generate unpredictable random sequences
Using srand((unsigned int)(time(NULL)) is one way, because the time to run the program is different each time.

Usage of random number generator in C: all current C compilers provide a pseudo-random number generator function based on ANSI standard to generate random Numbers. These are the rand() and srand() functions. The working process of these two functions is as follows:

1) First, a seed is provided to srand(), which is an unsigned int with values ranging from 0 to 65535.

2) It then calls rand(), which returns a random number (between 0 and 32767) based on the seed value provided to srand()

3) Rand () is called as many times as necessary to generate new random Numbers without interruption;

4) At any time, srand() can be given a new seed, further "randomizing" the output of rand().
Here is the random number program between 0 and 32767:


#include <stdlib.h>
#include <stdio.h>
#include <time.h>           //Use the current clock as the seed

void main( void )
{
  int i;
  srand( (unsigned)time( NULL ) );          //Initializes a random number
     for( i = 0; i < 10;i++ )                          //Print out 10 random Numbers
          printf( " %dn", rand() );
}

According to the above program, random Numbers between 0 and 1 can be easily obtained:

#include <stdlib.h>
#include <stdio.h>
#include <time.h> 
main( )
{
   int i;
   srand( (unsigned)time( NULL ) );  
       for( i = 0; i < 10;i++ )
            printf( "%5.2fn", rand()/32767.0);
}

Random Numbers between 1 and 100 can be written like this:

#include <stdlib.h>
#include <stdio.h>
#include <time.h> 
main( )
{
   int i;
   srand( (unsigned)time( NULL ) );  
       for( i = 0; i < 10;i++ )
            printf( "%dn", rand()%100+1);
}

Two, three general purpose random number generator, the third is recommended
Function name: rand
Power: random number generator
Void rand(void);
Application:

#include <stdlib.h> 
#include <stdio.h>

int main(void) 
{ 
   int i;

   printf("Ten random numbers from 0 to 99nn"); 
   for(i=0; i<10; i++) 
      printf("%dn", rand() % 100); 
   return 0; 
}

Function name: randomize this is better!
Power: initializes a random number generator
Void randomize(void);
Application:

#include <stdlib.h> 
#include <stdio.h> 
#include <time.h>

int main(void) 
{ 
   int i;

   randomize(); 
   printf("Ten random numbers from 0 to 99nn"); 
   for(i=0; i<10; i++) 
       printf("%dn", rand() % 100); 
   return 0; 
} 

Random number generation algorithm is introduced in common computer algorithms

How to generate random Numbers within the set range

Since rand generates random Numbers from 0 to rand_max, and rand_max is a large number, how do you generate Numbers from X to Y?

From X to Y, we have Y minus X plus 1, so to produce a number from X to Y, we just write:

K = rand () % (Y - X + 1) + X;

In this way, you can generate random Numbers in any range you want.

To generate random Numbers that do not repeat
Srand ((unsigned)time(NULL)); // initializes a random number


Related articles: