Sample sharing of generating random Numbers using the c language

  • 2020-04-02 02:16:30
  • OfStack

This is a code written in c that does not generate random Numbers repeatedly, and only input q can exit the program.


#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define MAX  100
int main(void)
{
 int i, j, flag, num, a[MAX] = { 0 }, max, ch;
 srand((unsigned)time(NULL));
 printf("Please input max number<1-39>(q to quit): ");
  a:while ((scanf("%d", &max)) == 1)
 {
  if (max >= 40)
  {
   printf("Please input max number<1-39>(q to quit): ");
   while (getchar() != 'n')
    continue;
   continue;
  }

  for (i = 0; i < max; ++i)
  {
   do{num = rand() % 41 + 1;
   }while(num==8||num==36);
   flag = 1;
   for (j = 0; j < i; ++j)
   {
    if (num == a[j] || num == 8 || num == 36)
    {
     flag = 0;
     break;
    }
   }
   if (flag)
    a[i] = num;
   else
    --i;
  }
  while 
   (getchar() != 'n') continue;
  for (i = 0; i < max; ++i)
   printf("%d ", a[i]);
  printf("n");
  printf("Please input max number<1-39>(q to quit): ");
 }
 if (getchar() != 'q')
 {
  puts("q to quit,please!");
  printf("Please input max number<1-39>(q to quit): ");
  while (getchar() != 'n')
   continue;
  goto a;
 }
 printf("Bye!");
 return 0;
}


Here is a more detailed explanation, you can have a look

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) firstly, a seed is provided to srand(), which is an unsigned int with values ranging from 0 to 65535;

2) then call 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 continuously obtain new random Numbers;

4) at any time, a new seed can be provided to srand() to further "randomize" 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
Function: random number generator
Usage: 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: random
Function: random number generator
Usage: int random(int num);
Application:


#include <stdlib.h> 
#include <stdio.h> 
#include <time.h>
 
int main(void) 
{ 
randomize(); 
printf("Random number in the 0-99 range: %dn", random (100)); 
return 0; 
} 


Function name: randomize this is better!
Function: initialize random number generator
Usage: 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.

Four, generate a random number that does not repeat
1)


#include <stdlib.h> 
#include <stdio.h> 
#include<stdio.h>
 #include <time.h>
swap(int *pm,int *pn)
{
int temp;
temp=*pm;
*pm=*pn;
*pn=temp;
}
int main(void)
{
inti,a[513];

srand( (unsigned)time( NULL ) ); 
for(i=1;i<=512;i++){a[i]=i;printf("%4d",a[i]);}
for(i=512;i>=1;i--)
{
 
swap(&a[i], &a[rand()%i+1]);

}
printf("n");
 for(i=1;i<=64;i++)
printf("%4d",a[i] );
getch();
}

2)


 #include <stdlib.h> 
#include <stdio.h> 
#include<stdio.h>
int main(void)
{
int a[100]={0};int i,m;
 for(i=1;i<=99;++i)
printf("%4d",a[i] );
srand( (unsigned)time( NULL ) );
for(i=1; i<=99; i++)
{
 while(a[m=rand()%100+1]);
 a[m] = i;
}
for(i=1;i<=99;++i)
printf("%4d",a[i] );
getch();
}


Related articles: