Rand and Srand

Source: Internet
Author: User
Tags define function generator rand

Http://www.cnblogs.com/lenient/articles/1565376.html


First we have a general view of Rand&srand: Srand initializes random seeds, Rand produces random numbers, which are described in detail below.

RAND (generating random numbers)

Table header file: #include <stdlib.h>

define function: int rand (void)

Function Description:

Because the internal implementation of Rand is done with linear congruential, he is not really a random number, just because its cycle is particularly long, so there is a certain range can be seen as random, Rand () will return a random value range between 0 to Rand_max. 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. Rand () produces false random numbers that are the same every time they are executed. To be different, initialize it with a different value. The Initialized function is Srand ().

return value:

Returns a random integer value between 0 and Rand_max, the Rand_max range is at least between 32767 (int), or double-byte (16 digits). If you use the unsigned int double byte is 65535, four bytes is an integer range of 4294967295.

0~rand_max the probability of each number being selected is the same.

Example:

/* Produces a random number between 1 and 10, this example does not set random numbers of seeds, complete random number generation please refer to

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);

}

}

Perform:

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 number Seed)

Table header file: #include <stdlib.h>

Definition function: void srand (unsigned int seed);

Function Description:

Srand () is used to set the random number seed for rand () to produce random numbers. The parameter seed must be an integer and can usually be used as seed by using the return value of Geypid () or time (0). If each seed is set to the same value, the random values generated by rand () will be the same each time.

Example

/* Produces a random number between 1 and 10, this example and the execution result can be used with the rand () reference * *

#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: Comparing with the Rand paradigm

5 8 8 8 10 2 10 8 9 9

2 9 7 4 10 3 2 10 8 7

Also or:

with "int x = rand ()% 100;" To generate random numbers from 0 to 100. This method is not or taken, and it is preferable to: j= (int) (N*rand ()/(rand_max+1.0)) produces a random number between 0 and N

int main (void)

{

int i;

time_t T;

Srand ((unsigned) time (&t));

printf ("Ten random numbers from 0 to 99\n\n");

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

printf ("%d\n", rand ()% 100);

return 0;

}

In addition to the above, the added point is that the Srand function must be placed outside the loop or circular call, otherwise the same number is obtained.

An example in MSDN.

Crt_rand.c

This program seeds the Random-number generator

With the time, then displays random integers.

//

#include <stdlib.h>

#include <stdio.h>

#include <time.h>

int main (void)

{

int i;

Seed the Random-number generator with

The numbers is different every time we run.

//

Srand ((unsigned) time (NULL));

Display ten numbers.

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

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

printf ("\ n");

Usually, you'll want to generate a number in a specific range,

such as 0 to:

{

int range_min = 0;

int range_max = 100;

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

{

int rand100 = ((double) rand ()/

(double) Rand_max) * Range_max + range_min);

printf ("%6d\n", rand100);

}

}

Summarize:

We know that 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 species, as a reference to a recursive formula for the calculation of a coefficient, when the series is very 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, when the computer boot, the value of the 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) function is

Initializes the initial value of the rand () function of a random generator, even if the value of the seed is changed to A; From this you can see that through the sand () function, we are able to produce predictable random sequences,

So how can we produce unpredictable random sequences? We may often need such random sequences, right. Using Srand ((unsign) (NULL) is a method, because the time of each run program is different, by the way, you know that the function is to return the number of seconds from 1970/01/01 to the present, which may not be the correct starting time, You check it out, right? C also provides another more convenient function, Randomize ()

The prototype is void Randomize (), which is the initial value of the seed of the rand (), and the value is indeterminate, which is equivalent to Srand ((Unsign) (Time (NULL)) but it should be noted that Randomize () function is implemented by time, so the header file should contain time.h when it is invoked.

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.