About rand () function reproduced in other people

Source: Internet
Author: User
Tags random seed

The rand () function in C + + Category: Programming languages /C #/articles

The generation of random number seeds in C + + is always confusing for beginners. As you know, there's a special Srand (N) function in C that can be easily implemented, but in C + + It's a bit more complicated. The following is the author learn a little bit of experience, I hope we can help. (We still need to use the rand () function in the C standard library)

function Description:

int rand (); : Returns a random integer from [0,max], where MAX is determined by the type of data you define; # include <cstdlib>

void Srand (unsigned seed); : Set random number seed, #include <cstdlib>

time_t time (time_t *time); : Returns the current time, #include <ctime>

application Example: 1): Srand (Time (0));                                       Sets the random number seed int i = rand ()% N according to the system time; Gets the integer of the interval [0,n]

To generate random numbers between 1~10, the code is as follows:

#include <iostream> using namespace std;

#include <ctime> #include <cstdlib>

int main () {int t;       Srand (Time (0));       Seed T = rand ()% + 1; Random number 1-10 cout << t << Endl;

return 0; }

2): Srand (Time (0));                                               //random numbers based on system time seed float x = rand () * x/ rand_max;                           //returns the probability of 1/x

3): Srand (Time (0));                                            Set the random number seed vector<int> v according to the system time;              Random access to array types, #include <vector> random_shuffle (V.begin (), V.end ()); STL algorithm Random_shuffle The order of elements in the container class

The following source code from crafty19.3, the strongest source of open chess program. The notes are clear, needless to say. Q: What is the 1.Knuth book about? I missed the book. 2.static const unsigned long x[55], what is the reason for taking 55 random numbers here? 3. Can you tell me more about the algorithms or theories generated by random numbers, or recommend some references? [Code]

unsigned int Random32 (void) {        static const unsigned long x[55] = {       1410651636ul, 3012776752UL, 3497475623UL, 2892145026UL, 1571949714UL,        3253082284ul, 3489895018UL, 387949491UL, 2597396737UL, 1981903553UL,        3160251843ul, 129444464UL, 1851443344UL, 4156445905UL, 224604922UL,        1455067070ul, 3953493484UL, 1460937157UL, 2528362617UL, 317430674UL,        3229354360UL, 117491133UL, 832845075UL, 1961600170UL, 1321557429UL,       747750121ul , 545747446UL, 810476036UL, 503334515UL, 4088144633UL,       2824216555ul, 3738252341UL, 3493754131UL, 3672533954UL, 29494241UL,       1180928407ul, 4213624418UL, 33062851UL, 3221315737UL, 1145213552UL,       2957984897ul, 4078668503UL, 2262661702UL, 65478801UL, 2527208841UL,       1960622036ul, 315685891UL, 1196037864UL, 804614524UL, 1421733266UL,       2017105031ul, 3882325900UL, 810735053UL, 384606609UL, 2393861397UL};     static int init = 1;     static unsigned long y[55];     static Int J, K;     unsigned Long ul;       if (init)     {      int i;            init = 0;       for (i = 0; i <; i++) y[i] = X[i];       j = 24-1;       k = 55-1;     }       ul = (Y[k] + y[j]);     if (--j < 0) j = 55-1;     if (--k < 0) k = 55-1;     return ((unsigned int) ul); } [/code]

For beginners, just master 1 of the usage, a deeper level of ascension with the natural will have some understanding.


One, C + + cannot use the random () function the random function is not ANSI C standard, can not be compiled under the compiler GCC,VC and so on.      You can use the Rand function under C + + to implement it.  1. The C + + standard function Library provides a random number generator RAND, which returns a pseudo-random integer evenly distributed between 0-rand_max. The Rand_max must be at least 32767. The rand () function does not accept parameters and defaults to 1 as the seed (that is, the starting value). The random number generator always starts with the same seed, so the pseudo-random sequence is the same and loses its random meaning. (But this is easy for program debugging) 2, another function Srand () in C + +, you can specify a different number (unsigned integer variable) to seed. But if the seed is the same, the pseudo-random sequence is the same.       One way is to let the user enter the seed, but still not ideal. 3, the ideal is to use the number of changes, such as time to be a random number generator seed. The value of time is different every moment. So the seed is different, so the random number produced is different. C + + random function (VC program) #include <stdio.h> #include <iostream> #include <time.h> using  namespace Std; #define MAX. int main (int argc, char* argv[]) {

The Srand () function produces a random seed that starts at the current time.

It should be in front of the for-and-wait loop, or it'll take a long time.

Srand ((unsigned) time (NULL));

for (int i=0;i<10;i++) Cout<<rand ()%max<<endl;//max is the maximum value, and its random field is 0~max-1 return 0; The usage of rand () rand () does not require arguments, it returns an arbitrary integer from 0 to the maximum random number, and the maximum random number size is usually a fixed large integer.       Thus, if you want to produce a 0~10 of 10 integers, you can express it as: int N = rand ()% 11;   Thus, the value of N is a random number of 0~10, if it is to produce 1~10, then it is: int N = 1 + rand ()% 11;   In summary, it can be expressed as: A + rand ()% n where a is the starting value and N is the range of integers. A + rand ()% (b-a+1) represents a random number between a~b to 0~1 decimal, you can get the 0~10 integer, and then divide by 10 to obtain a random decimal to a very bit of 10 random decimals, you need to get a random decimal to the percentile of the first 0~       100 of 10 integers, then all divided by 100, and so on. Usually rand () generates random numbers that are the same as the last time they were run, and this is intentionally designed to facilitate debugging of the program.       To produce a different random number each time, you can use the Srand (seed) function to randomize, and with different seed, you can produce different random numbers. As you may say, you can also include the time.h header file, and then use Srand (Time (0)) to randomize the random number generator using the current times, which guarantees that a different sequence of random numbers can be obtained every two runs (as long as the two runs are more than 1 seconds apart).

Original URL http://verydemo.com/demo_c92_i190538.html

About rand () function reproduced in other people

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.