A method for generating random numbers in C language

Source: Internet
Author: User
Tags define function

A method for generating random numbers in C language

One

//************************************

Pseudo-Random number

In the C language, the rand () function can be used to generate random numbers, but this is not the true meaning of the random number, is a pseudo-random number, is based on a number, we can call it a seed, as a reference to a recursive formula derived from a coefficient, when the series number is large, it is normal distribution, This is equivalent to generating a random number, but this is not a true random number.

RAND (generates random numbers)
Table Head #include<stdlib.h>
define function int rand (void)
Function description
RAND () returns a random number, ranging from 0 to Rand_max. 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.
return value
Returns a random number between 0 and Rand_max, Rand_max defined in Stdlib.h, with a value of 2147483647.


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


int main (void)
{
for (int i=0;i<10;i++)
{
printf ("%d\n", Rand ());
}
return 0;
}

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

Generate unforeseen random numbers
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. The work of these two functions is 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 Srand () is used to set the random number seed when rand () produces a random number. Before calling the rand () function to produce a random number, the random number seed (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.
The above example is because no random number seed is set, and each random number seed is automatically set to the same value of 1, which results in the same random values generated by rand ().

Srand () function:
RAND (set random number Seed)
Table header file #include <stdlib.h>
Defines the function void Srand (unsigned int seed);
Function description
The Srand () is used to set the random number seed when rand () produces a random number. The parameter seed must be an integer and can usually be used as seed using the return value of Geypid () or time (0). If each seed is set to the same value, rand () produces a random number that is the same every time.

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

int main (void)

{
Srand (Time (0)); Time (0)/time (NULL) returns the current times of the system
for (int i=0;i<10;i++)
{
printf ("%d\n", Rand ());
}
return 0;
}

//************************************
Get random numbers with different requirements

1), to obtain the number within a certain range, directly do the corresponding division to take surplus can be
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?
Just write it like this:
Rand ()% (y-x+1) +x;
This can produce random numbers in any range.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main (void)

{
Srand (Time (0));
for (int i=0;i<10;i++)
{//generates an integer within 10
printf ("%d\n", Rand ()%10);
}
return 0;
}
2), get decimals? For example: We can get an integer within 10001 (0~10000), and then divide the integer by 10000 to get the decimal of two digits after the decimal point.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main (void)

{
Srand (Time (0));
for (int i=0;i<10;i++)
{
printf ("%f\n", (rand ()%10001)/10000.0);
}
return 0;
}

//************************************

Second, 3 General random number generator, recommended with the 3rd
Name of function: rand ()
Function: Random number generator
Usage: void rand (void);

Function name: Random ()
Function: Random number generator
Usage: int random (int num);
Returns a random number between 0~num-1. The random (NUM) is a macro definition in stdlib.h. Both NUM and function return values are integral types.
program Example:

#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 ()
Prototype: int randomize (void)
Function: Use the time function to get a random number
Header files: stdlib.h time.h
This function is equivalent to Srand ((Unsign) (Time (NULL)) but it should be noted that the function of Randomize () is implemented by time so
When you call it, the header file contains time.h.

program Example:

#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;
}

A method for generating random numbers in C language

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.