The C language uses the rand () function to implement random (int m), randrandom

Source: Internet
Author: User
Tags define function random seed

The C language uses the rand () function to implement random (int m), randrandom

The rand () function is a real random number generator, and srand () sets the random number seed for rand. If you did not call srand () before calling rand () for the first time, the system will automatically call srand () for you ().

Note that calling rand () with the same number of seeds will generate the same random number sequence.

Therefore, to generate a seed with m as a random number, we must first call srand ();

The following example shows how to generate a random number of 10 with 100 as the seed and control them to 0 ~ Within 100.

# Include <stdio. h> int random (int m) {return rand () % m; // control the random number to 0 ~ } Int main () {int n = 10, I, m = 100, s = 50; srand (s); // set the Random Seed for (I = 0; I <n; I ++) {printf ("% d", random (m);} printf ("\ n"); return 0 ;}/ * is frequently used: srand (time (NULL); to generate a random number with the current time as the seed, which must contain <time. h> */


C language rand () function

Rand and srand usage
First, we need to have a general opinion on rand & srand: srand initializes random seeds, and rand generates random numbers. The following describes in detail.

Rand (Random Number Generation)
Header file: # include <stdlib. h>

Define function: int rand (void)

Function Description:
Because the internal implementation of rand is made by the linear same remainder method, it is not a real random number, but because its cycle is very long, so it can be considered as random within a certain range, rand () returns a random value ranging from 0 to RAND_MAX. Before calling this function to generate a random number, you must use srand () to set the random number seed. If no random number seed is set, rand () will automatically set the random number seed to 1. Rand () generates false random numbers, which are the same during each execution. To be different, initialize it with different values. The initialized function is srand ().

Return Value:
Returns a random integer between 0 and RAND_MAX. The range of RAND_MAX is at least 32767 (int), that is, double byte (16 digits ). If unsigned int is used, the dual-byte value is 65535, and the four-byte value is an integer range of 4294967295.
0 ~ RAND_MAX the probability of each number being selected is the same.

Example:
/* Generate a random value ranging from 1 to 10. In this example, no random seed is set. For the complete random number generation, see
Srand ()*/
# Include <stdlib. h>
Main ()
{
Int I, j;
For (I = 0; I <10; I ++)
{
J = 1 + (int) (10.0 * rand ()/(RAND_MAX + 1.0 ));
Printf ("% d", j );
}
}
Run:

9 4 8 8 10 2 4 8 3 6
9 4 8 8 10 2 4 8 3 6 // re-execution still produces the same random number

Srand (set Random Seed)
Header file: # include <stdlib. h>

Define the function: void srand (unsigned int seed );

Function Description:
Srand () is used to set the random number seed when rand () generates a random number. The seed parameter must be an integer. Generally, the return value of geypid () or time (0) can be used as seed. If the same value is set for each seed, the random values generated by rand () are the same each time.

Example
/* Generate a random number ranging from 1 to 10. This example and execution result can be referenced with rand */
# Include <time. h>
# Include <stdlib. h>
Main ()
{
Int I, j;
Srand (int) time (0 ));
For (I = 0; I <10; I ++)
{
J = 1 + (int) (10.0 * rand ()/(RAND_MAX + 1.0 ));
Printf ("% d", j );
}
}
Execution: Compare with the rand example
5 8 8 8 10 2 10 8 9 9
2 9 7 4 10 3 2 10 8 7
Or:
Use "int x = rand () % 100;" to generate a random number between 0 and 100. This method is not or can be used. A better method is j = (int) (n * rand ()/(RAND_MAX + 1.0) generates a random number between 0 and n.
Int main (void)
{
Int I;
Time_t t;
Srand (unsigned) time (& t ));
Printf ("Ten r... the remaining full text>

How to Use the rand () function in C language?

Rand (Random Number Generation)
Related functions
Srand

Header file
# Include <stdlib. h>

Define functions
Int rand (void)

Function Description
Rand () returns a random value ranging from 0 to RAND_MAX. Before calling this function to generate a random number, you must use srand () to set the random number seed. If no random number seed is set, rand () will automatically set the random number seed to 1. For details about random seed, see srand ().

Return Value
Returns a random value between 0 and RAND_MAX. RAND_MAX is defined in stdlib. h and its value is 2147483647.

Example
/* Generate a random value ranging from 1 to 10. In this example, no random seed is set. For the complete random number generation, see
Srand ()*/
# Include <stdlib. h>
Main ()
{
Int I, j;
For (I = 0; I <10; I ++)
{
J = 1 + (int) (10.0 * rand ()/(RAND_MAX + 1.0 ));
Printf ("% d", j );
}
}

Run
9 4 8 8 10 2 4 8 3 6
9 4 8 8 10 2 4 8 3 6

Srand (set Random Seed)
Related functions
Rand

Header file
# Include <stdlib. h>

Define functions
Void srand (unsigned int seed );

Function Description
Srand () is used to set the random number seed when rand () generates a random number. The seed parameter must be an integer. Generally, the return value of geypid () or time (0) can be used as seed. If the same value is set for each seed, the random values generated by rand () are the same each time.

Return Value

Example
/* Generate a random number ranging from 1 to 10. This example and execution result can be referenced with rand */
# Include <time. h>
# Include <stdlib. h>
Main ()
{
Int I, j;
Srand (int) time (0 ));
For (I = 0; I <10; I ++)
{
J = 1 + (int) (10.0 * rand ()/(RAND_MAX + 1.0 ));
Printf ("% d", j );
}
}

Run
5 8 8 8 10 2 10 8 9 9
2 9 7 4 10 3 2 10 8 7

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.