C++rand () function-produce random number __ function

Source: Internet
Author: User
Tags generator rand random seed

C + + produces random number of seeds for beginners have been very confused. As you know, there is a special srand (N) function in C that can be easily implemented, but it is more complex in C + +. The following is the author to learn a little experience, hope for everyone 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 required

void Srand (unsigned seed);
: Set random number seed, #include time_t time (time_t *time);
: Returns the current time, #include application example: 1): Srand (0);
Set random number seed int i = rand ()% N based on system time
To get an integer of interval [0,n] to produce a random number 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 ()% 10+ 1;       Random number 1-10

         cout << t << Endl;
         return 0;
}

2): Srand (Time (0));
Set random number seed float x = rand () * X/rand_max based on system time;
Probability of returning 1/x 3): Srand (Time (0));
The seed vector v of random number is set according to the system time;
Random access array type, #include random_shuffle (V.begin (), V.end ());
STL algorithm Random_shuffle The element order of the container class

The following source code from crafty19.3, the most powerful source of open chess program. The notes are clear and needless to say. Ask:
What is the 1.Knuth book about? I can't read the book.
2.static const unsigned long x[55], what's the reason for taking 55 random numbers here?
3. Can you speak more comprehensively about some algorithms or theories of random number generation, or recommend some reference materials.

[Code]
unsigned int Random32 (void) {

static const unsigned long x[55] = {1410651636UL, 3012776752UL, 3497475623UL, 2892145026UL, 1571949714UL, 3253082284U L, 3489895018UL, 387949491UL, 2597396737UL, 1981903553UL, 3160251843UL, 129444464UL, 1851443344UL, 4156445905UL, 224604 922UL, 1455067070UL, 3953493484UL, 1460937157UL, 2528362617UL, 317430674UL, 3229354360UL, 117491133UL, 832845075UL, 19 61600170UL, 1321557429UL, 747750121UL, 545747446UL, 810476036UL, 503334515UL, 4088144633UL, 2824216555UL, 3738252341UL , 3493754131UL, 3672533954UL, 29494241UL, 1180928407UL, 4213624418UL, 33062851UL, 3221315737UL, 1145213552UL, 29579848 97UL, 4078668503UL, 2262661702UL, 65478801UL, 2527208841UL, 1960622036UL, 315685891UL, 1196037864UL, 804614524UL, 14217
33266UL, 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, the deeper level of ascension naturally will be understood.
Other:

One, C + + can not use the random () function random function is not ANSI C standard, can not be compiled in GCC,VC and other compiler. You can use the RAND function under C + + instead. 1. 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 arguments, and the default is 1 seed (that is, the starting value). Random number generators always start with the same seed, so the pseudo-random sequence is the same, and the random meaning is lost. (but this is convenient for program debugging)
2, another function in C + + Srand (), you can specify a different number (unsigned integer variable) as a seed. But if the seeds are the same, the pseudo-random sequence is the same. One way is to let the user input the seed, but still not ideal.
3, the more ideal is to use the number of changes, such as time as a random number generator seed. The value of time is different every moment. So the seeds are different, so the random numbers that are produced are 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[])
{
     //srand () function produces a random seed starting at the current time.
     Should be placed in front of a for-wait loop statement otherwise it will take a long time to wait for
     Srand ((unsigned) times (NULL));
for (int i=0;i<10;i++)
Cout<<rand ()%max<<endl;//max is the maximum and its random field is 0~max-1 return
0;
}

II, RAND ()
rand () does not require a parameter, it returns an arbitrary integer from 0 to the largest random number, and the maximum random number is usually a fixed large integer. So, if you want to produce a 0~10 10 integers, it can be expressed as:
int N = rand ()% 11;   
this way, the value of N is a 0~10 random number, and if you want to produce a 1~10, 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 decimals, you can get 0~10 integers, and then divide by 10 to obtain random to the very bit of 10 random decimal places, to get random decimal digits, you need to get 0~ 100 of the 10 integers, then all divided by 100, and so forth. The
usually the random number generated by rand () is the same as the previous one at each run, intentionally designed to facilitate debugging of the program. To produce a different random number at a time, you can use the Srand (
Seed) function for randomization, which can produce different random numbers, depending on the seed.
As you have said, you can also include the time.h header file, and then use Srand (0) to randomize the random number generator using the current time, so that you can get a different sequence of random numbers every two times (as long as two runs at intervals of more than 1 seconds).

Related Article

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.