C-language random function

Source: Internet
Author: User

In C, the rand () function can be used to generate a random number, but this is not a random number in the true sense, it is a pseudo-random number, is a number, we can call it a seed, as a reference to a recursive formula derived from a coefficient, when the series number is very large, it is consistent with the normal publication, This is equivalent to generating a random number, but this is not a true random number, when the computer is properly booted, the value of this seed is fixed, unless you destroy the system, in order to change the value of this seed, C provides the Srand () function, its prototype is void Srand (int a).


It may be known that random functions in the C language are arbitrary but the random function is not the ANSI C standard, so the random function cannot be compiled by GCC,VC and other compilers.

RAND () returns a random number, ranging from 0 to Rand_max. Returns a random value 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 number, which depends on the type of variable you define, and int is 32767. Before calling this function to produce a random number, the random number seed must be set with Srand (), and if no random number seed is set, rand () automatically sets the random number seed to 1 when called. You typically use the For statement to set the number of seeds. See the example below.

How to produce unforeseen random sequences?

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

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

1) First give Srand () 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 a new random number without interruption;

4) Whenever possible, a new seed can be given to Srand () to further "Randomize" the output of rand ().


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


#include


#include


#include//seeding with current clock


void Main (void)

{

int i;

Srand ((unsigned) time (NULL)); Initialize random number

for (i = 0; i < 10;i++)//Print out 10 random numbers

printf ("%d\n", Rand ());

}


Depending on the program above, it is easy to get the random number between 0~1:


#include


#include


#include


Main ()

{

int i;

Srand ((unsigned) time (NULL));


for (i = 0; i < 10;i++)

printf ("%5.2f\n", rand ()/32767.0);

}


The resulting random number between 1~100 can be written like this:


#include


#include


#include


Main ()

{

int i;

Srand ((unsigned) time (NULL));


for (i = 0; i < 10;i++)

printf ("%d\n", Rand ()%100+1);

}


Two or three general-purpose random number generator, it is recommended to use a third

Name of function: Rand


Function: Random number generator


Usage: void rand (void);


program Example:

Copy the code code as follows:


#include


#include


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: Initialize random number generator


Usage: void randomize (void);


program Example:

Copy the code code as follows:


#include


#include


#include


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 is introduced in the computer common algorithm.

Iii. how to generate random numbers within a set range

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

From X to Y, there are the number of y-x+1, so to produce a number 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 to produce a random number that is not duplicated

Srand ((unsigned) time (NULL)); Initialize random number


C-language random function

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.