Random number: rand ()

Source: Internet
Author: User
Tags define function

first we have a general view of Rand&srand: Srand initializes random seeds, Rand generates random numbers, which are explained in detail below.  rand (generates random numbers) table header files: #include <stdlib.h> define function: int rand (void) function Description: because Rand's internal implementation is done linearly with congruential, he is not really a random number, only because of its exceptionally long period, so that a certain range can be seen as random, and Rand () returns a random number, ranging between 0 and 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. Rand () produces false random numbers that are the same each 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, with a range of Rand_max at least between 32767 (int), or double-byte (16-digit). If the unsigned int double byte is 65535, four bytes is the integer range of 4294967295.  0~rand_max The odds of each number being selected are the same.  Example: /* Generate random numbers from 1 to 10, this example does not have random number 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); } } Execution:   9 4 8 8 2 4 8 3 6 9 4 8 8 10 2 4 8 3 6///execute again the same random number is still generated   Srand (set random number Seed) table header files: #include <stdlib.h> define 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.  Example / * Generates random values between 1 and 10, this example and the execution result can be compared 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: Comparison with Rand example   5 8 8 8, 2 8 9 9 2 9 7 4 3 2 ten 8 7 or: with "int x = rand ()% 100;" To generate a random number from 0 to 100 this method is not or fetch, it is preferable that: 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 ()%); return 0; } In addition to what is said above, it is srand that the function must be placed outside the loop or the outside of the loop call, otherwise the same number is obtained.  an example from MSDN.  //CRT_RAND.C //This program seeds the Random-number generator //With the time and then displays random integers. // #include <stdlib.h> #include <stdio.h> #include <time.h> int main (void) { int i;    //Seed the Random-number generator with current time so that //The numbers'll be different every time we run.     // Srand ((unsigned) time (NULL)); //Display 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 = +; For (i = 0; I < 10; i++)        { int rand100 = ((double) rand ()/ (double) rand_max) * Range_max + range_min); printf ("%6d\n", rand100);        }    } Summary: we know that the rand () function can be used to generate random numbers, but this is not a random number in the true sense, it is a pseudo-random number, it is based on a number, we can call it seed, a coefficient derived from a recursive formula for the benchmark, when the series number is large, it conforms to the normal publication, Thus the equivalent of generating a random number, but this is not a true random number, when the computer normally boot, 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) function is Initializes the initial value of the rand () function of the random generator, even if the value of the seed is changed to A; From this you can see through the sand () function, we are able to produce predictable random sequences, So how do we generate unforeseen random sequences? We may often need such random sequences, right. The use of Srand ((Unsign) (Time (NULL)) is a method, because the timing of each run of the program is different, and you know the function of the Times () 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, right? C also provides another more convenient function, randomize () The prototype is void Randomize (), the function is the initial value of the seed of the beginning rand (), and the value is indeterminate, it is equivalent to Srand ((Unsign) (Time (NULL)) but it should be noted that Randomize () function to be implemented by time, so the header file contains time.h when it is called.

Random number: rand ()

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.