Examples of generating random numbers using C language sharing _c language

Source: Internet
Author: User
Tags generator rand

This is a C-write code that produces random numbers without duplication, and only input Q can exit the program.

Copy Code code as follows:

#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 description, you can see

In the C language, the rand () function can be used to generate random numbers, but that's not true. A random number in the sense of a pseudo random number, is based on a number, we can call it a seed, as a reference to a recursive formula to calculate a coefficient, when the series is very large, it is consistent with normal publication, Which is equivalent to generating a random number, but this is not a real random number, the value of the seed is fixed when the computer is powered up, unless you destroy the system, in order to change the value of the seed, C provides the Srand () function, its prototype is void Srand (int a).

Maybe everyone knows the random function random in C, but the random function is not ANSI C standard, so the random function cannot be compiled and passed by compiler such as GCC,VC.

RAND () returns a random value range between 0 and Rand_max. Returns a random number between 0 and Rand_max, Rand_max defined in Stdlib.h, with a value of at least 32767. The result of my operation is an indefinite amount, which depends on the type of variable you define, and the int plastic is 32767. Before calling this function to produce random numbers, you must first use Srand () to set up the random number of seeds, if no random number of seeds, rand () in the call will automatically set the random number of seed 1. You typically use a For statement to set the number of seeds. See the example below for details.

How does one produce unpredictable random sequences?

The use of srand (unsigned int) (Time (NULL)) is a method, because the timing of each run of the program is different.

The use of random number generators provided in C language: The C compiler now provides a pseudo random number generator function based on ANSI standard, which is used to generate random numbers. They are the rand () and Srand () functions. These two functions work in the following process:

1 first to Srand () to provide a seed, it is a unsigned int type, its value range from 0~65535;

2) then call Rand (), which returns a random number (between 0 and 32767) based on the seed value provided to Srand ()

3 Call rand () as many times as necessary to obtain new random numbers without interruption;

4 any time, you can give Srand () a new seed, thereby further "randomization" rand () output results.

The following is a random number program between 0~32767:

Copy Code code as follows:

#include <stdlib.h>
#include <stdio.h>
#include <time.h>//Use the current clock to make seeds

void Main (void)
{int i;
Srand ((unsigned) time (NULL)); Initializing random numbers
for (i = 0; i < 10;i++)//Print out 10 random numbers
printf ("%d\n", Rand ());
}

Depending on the program above, you can easily get random numbers between 0~1:

Copy Code code as follows:

#include <stdlib.h>
#include <stdio.h>
#include <time.h>
Main ()
{int i;
Srand ((unsigned) time (NULL));
for (i = 0; i < 10;i++)
printf ("%5.2f\n", rand ()/32767.0);
}

The random number that produces 1~100 can be written like this:

Copy Code code as follows:

#include <stdlib.h>
#include <stdio.h>
#include <time.h>
Main ()
{int i;
Srand ((unsigned) time (NULL));
for (i = 0; i < 10;i++)
printf ("%d\n", Rand ()%100+1);
}

Two, three universal random number generators, recommended with a third

Function Name: Rand
Function: Random number generator
Usage: void rand (void);
program Example:

Copy Code code as follows:

#include <stdlib.h>
#include <stdio.h>
int main (void)
{
int i;
printf ("Ten random numbers from 0 to 99\n\n");
for (i=0; i<10; i++)
printf ("%d\n", rand ()% 100);
return 0;
}

Function Name: Random
Function: Random number generator
Usage: int random (int num);
program Example:

Copy Code code as follows:

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

/* Prints a random number in the range 0 to 99 * *
int main (void)
{
Randomize ();
printf ("Random number in the 0-99 range:%d\n", Random (100));
return 0;
}



Function Name: Randomize This is better!
Function: Initialization of random number generator
Usage: void randomize (void);
program Example:
Copy Code code as follows:

#include <stdlib.h>
#include <stdio.h>
#include <time.h>
int main (void)
{
int i;
Randomize ();
printf ("Ten random numbers from 0 to 99\n\n");
for (i=0; i<10; i++)
printf ("%d\n", rand ()% 100);
return 0;
}

The algorithm of generating random numbers in the computer algorithm is introduced

How to generate random numbers within a set range

Since Rand produces random numbers from 0 to Rand_max, and Rand_max is a very large number, how do you generate numbers from X~y?

From X to Y, there are y-x+1 numbers, so to produce numbers from X to Y, you only need to write this:

Copy Code code as follows:

K=rand ()% (y-x+1) +x;

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

Four, producing a random number that is not repeated
1)

Copy Code code as follows:

#include <stdlib.h>
#include <stdio.h>
#include <stdio.h>
#include <time.h>
Swap (int *pm,int *PN)/* must be exchanged with pointers */
{
int temp;
TEMP=*PM;
*PM=*PN;
*pn=temp;
}

int main (void)
{
INTI,A[513];
/*int *pa,*pb;*/
Srand ((unsigned) time (NULL)); /* Define this to produce a different random number * *
for (i=1;i<=512;i++) {a[i]=i;printf ("%4d", A[i]);}
for (i=512;i>=1;i--)
{
/* pa=&a[i]; Pb=&a[rand ()%i+1];*/
Swap (&a[i], &a[rand ()%i+1]);/* Plus one is random from one to I, and will not contain 0*/
/* No more definition of pointers, so the conclusion is the same * *
}
printf ("\ n");
for (i=1;i<=64;i++)
printf ("%4d", A[i]);
Getch ();/*WINTC Output * *
}

2)

Copy Code code as follows:

#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 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.