Srand () and rand () function usage

Source: Internet
Author: User
Tags random seed

Srand () and rand () function usage From: http://blog.csdn.net/zqy2000zqy/article/details/1174978 Srand () is to provide seed for rand ().

If srand inputs the same value each time, the random number generated each time is the same,

Srand (N)

For (10)

Rand ()
That is to say, using a fixed value as the seed is a disadvantage. The common practice is to use this sentenceCodeSrand(Unsigned) Time (null ));This will make the seed a non-fixed number, so that the random number will not be the same for each execution.


1. Let's look at an example. 
# Include <iostream> 
# Include <stdlib. h> 
# Include <time. h> 
Using namespace STD; 
Int main (void) 

Int I; 
/* Seed the random-number generator with current time so that 
* The numbers will be different every time we run. 
*/ 
Srand (unsigned) Time (null )); 
/* Display 10 numbers .*/ 
For (I = 0; I <10; I ++) 
Printf ("% 6D/N", Rand ()); 
}

2. About time. h 
Time. h contains many interesting functions, such 
Char * ctime (long * Clock) 
This function converts the time specified by clock (such as the time returned by the function time) 
String: Mon Nov 21 11:31:54, 1983/n/0

# I nclude <iostream> 
# I nclude <stdlib. h> 
# I nclude <time. h> 
Using namespace STD;

Void main () 

Time_t T1, T2; 
Char gettime [20]; 
Char * ptstring = gettime;

Int X, Count = 0; 
X = rand_max; 
Cout <'/N '; 
T1 = Time (null ); 
Ptstring = ctime (& T1 );

While (count <= 100) 

Srand (unsigned) Time (null )); 
X = rand () % 50; 
If (x <5) 
Continue; 
Else 

Count ++; 
Cout <"The numth is" <'/N '; 

}

When you view the ptstring value, "Tue Sep 13 16:31:06 2005" is displayed"

3. Finally, let's talk about the srand () function. 
Void srand (unsigned seed) initialize the random number generator

Some discussions are as follows: 
1. The C function library does not directly put the important operations for initializing random seeds using the system clock into the ran

In the implementation of the D function, I think there are at least three reasons: 
(1) continuous random numbers can be generated efficiently without every initialization; 
(2)ProgramPersonnel with higher flexibility, because it is possible to use better data in scenarios with higher requirements

Seed, not the system clock; 
(3) initialization is not required to generate a large number of pseudo-random numbers for some verification or statistics.

Every time the program runs, it generates the same series of random numbers. In some cases, this does not matter.

In fact, there is a more important reason: 
The rand () function of the pseudo-random sequence generator must have an important feature:

Columns must be reproducible. 
This is not justAlgorithmTo a large extent, it is related to the accuracy of code testing. If the algorithm

Using data related to the results of rand (), we have the opportunity to reproduce each

Test process to locate the problem more effectively. 
So here is a suggestion. in the Code, if the result of the Rand () function is related to the result of the algorithm

Make sure that your rand () call is reproducible.

4,C LanguageUse of the Rand () and srand () functions in --

Rand (void) is used to generate a pseudo random unsigned int integer. 
Srand (SEED) is used to set seeds for the rand () function.

Srand and Rand should be set and used. In general, srand is used to set the Rand. 
For example: 
Srand (uint) getcurrenttime ()); 
Int x = rand () %100; 
Is to generate a random number between 0 and 100.

Srand () is used to initialize the random number of seeds, because the internal implementation of Rand is done by the linear same remainder method, which is not true.

The random number is only because its cycle is particularly long. Therefore, it can be regarded as random within a certain range. The formula is as follows:


Rand = Rand * const_1 + c_var; 
The srand function is the First Rand value for it.

Use "int x = rand () % 100;" to generate a random number between 0 and 100. This method is optional, 
A good practice is: J = (INT) (N * rand ()/(rand_max + 1.0) generate a random number between 0 and N.

Quantity
Rand_max = 0x7fffffff

5. Summary 
1) srand () provides seeds for rand () 
2) seed in srand () is generally obtained by the time function. For example, srand (uint) getcurrenttime () srand (u

Nsigned) Time (null) Time () function to get the current system time...

Certificate -------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Srand () and rand ()

From: http://hi.baidu.com/%B4%FA%CD%AC%BD%DC/blog/item/405f77efaede42f1b3fb95e3.html


There are srand and Rand functions. The srand function is used to initialize the rand function, but its interpretation is vague. The following examples describe their usage:

Rand () usage:

# Include
# Include
Main (){
IntI;
I = rand (); // generates any value from 0-rand_max (infinity)

J = rand () % 6 + 1; // indicates generating a random number from 1 to 6
Printf ("% d", I );
}
If the program is printed for the first time5In the future, as long as you run this program, all the prints will be5;

Srand () usage:

IfI = rand (); If srand (Time (null) is added before the statement for initialization with the system time, different print results will be generated, as long as the srand () parameters are different, different random numbers are generated.

Srand () is used to set random seeds. Different seeds generate different random number sequences. Otherwise, each operation is the same sequence. Of course, the seeds are also random, therefore, time is generally used as the seed.

Srand ()UsageExplanation:

In C ++/C, there is no way to obtain a real random number sequence. to wait for a real random number sequence, a specific random number hardware generator must be used. that is to say, the software cannot generate real random numbers. because the software must be written according to certain logic. since it is written according to the specific logic (that is, the algorithm), the calculation result is certain. this is the inherent feature of software. think about it. If the results of each running of a software with the same code are different, who else will use it?

Therefore, in C ++/C, the concept of "pseudo-random number" is introduced. this means that a specific algorithm is used to generate a pseudo random number sequence. the programmer wants this random number sequence to be different from the real random number sequence, so he has a concept of "seeding.

Srand (unsgined int seed );

This function is used for "seeding. A seed is used to control the sequence of random numbers. as long as the seed is different, the random number sequence obtained through RAND () is different. conversely, if the seeds are the same, the random numbers obtained through srand () are the same.

Srand (0 );
For (INT I = 0; I <10; I ++)
{
Cout <rand () <'';
}

You try to execute this program twice and you will find that the results are the same twice. That's because, once the "Seed" is determined, the random number sequence is determined. The inherent "behavior repeatability" of Software determines this.

Therefore, a random seed is usually used during seeding. in most cases, the current system time is used. this number is different every time the program runs. unless you manually Change the system time.

// Original:Http://blog.sina.com.cn/s/blog_5218d56201009i7f.html

# Include <iostream>
# Include <time. h>
Using namespace STD;

Int randnumber (){
Int I;
Rand ();
Srand (unsigned) Time (null ));
I = rand () % 100;
Return I;
}

Int main ()
{
Int A, B;
// Srand (unsigned) Time (null); // srand has different results at two different positions

A = randnumber ();
B = randnumber ();
Cout <"A =" <A <"\ n"
<"B =" <B <"\ n ";

Return 0;
}

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.