Using detailed _c language in C language based on random function

Source: Internet
Author: User
Tags generator rand

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: 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:

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

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

Four in order to produce a random number without repetition
Srand ((unsigned) time (NULL)); Initializing random numbers

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.