Use of the random number rand () and srand () Functions

Source: Internet
Author: User
Tags define function random seed

Excerpted from: Kz= 356105289


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 particularly long, so it can be viewed 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.(Including 0 and rand_max), Rand_max is at least in the range of 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.

In the source code, I view the following code: # define rand_max 0x7fff, that is, 32767

/* 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> int main () {int I, j; for (I = 0; I <10; I ++) {j = 1 + (INT) (10.0 * rand () /(rand_max + 1.0); printf ("% d", j) ;}// execute: 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 Seed when rand () generates a random number. The seed parameter must be an integer. Generally, the return value of geypid () or time (null) 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.

/* Generates a random number ranging from 1 to 10. This example and execution result can be referenced with RAND () */# include <time. h> # include <stdlib. h> int main () {int I, j; srand (unsigned INT) Time (null); for (I = 0; I <10; I ++) {j = 1 + (unsigned INT) (10.0 * rand ()/(rand_max + 1.0); printf ("% d", j) ;}// execute: comparison with the Rand example (the random number generated for each execution is different) // 5 8 8 8 10 2 10 8 9 9/2 9 7 4 10 3 2 10 8 7


Note: using "x = rand () % 100;" to generate a random number between 0 and 99 is not advisable. A better method is J = (INT) (N * rand ()/(rand_max + 1.0) generates a random number between 0 and n-1.

In addition, the srand function must be placed outside the loop or called cyclically. Otherwise, the same number is obtained.

#include <time.h>int main(void){    int i;    srand((unsigned int)time(NULL));    for(i=0; i<10; i++)        printf("%d ", rand()%100);}


We know that the rand () function can be used to generate random numbers, but this is not a real random number, it is a pseudo random number, it is based on a number, we can call it a kind, A coefficient calculated based on a recursive formula. When the number of series is large, it is in line with the normal announcement, which is equivalent to generating a random number, but this is not a real random number, after the computer starts up normally, the seed value is fixed. Unless you break the system, C provides the srand () function to change the seed value, its prototype is void srand (int A). The function is
Initialize the initial values of the random generator rand () function, even if the seed value is changed to a. From this you can see that through the sand () function, we can generate foreseeable random sequences,
Then how can we generate unforeseen random sequences? We may often need such a random sequence, right. Srand (unsign) (Time (null) is a method, because the time of each running program is different, right, You know time () the function returns the number of seconds from to the present. C also provides another more convenient function. The original form of randomize () is void randomize (), the function is used to start the initial value of the Rand (), and the value is uncertain. It is equivalent to srand (unsigned) (Time (null )) however, it should be noted that the randomize () function should be implemented through time, so the header file should contain time when calling it. H.

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