C + + random number generation example to explain _c language

Source: Internet
Author: User
Tags rand

What would you do if you had to use C + + to generate random numbers between 0--n-1? You might say, quite simply, look:

Srand ((unsigned) time (NULL));
Rand ()% N;

Think about it, is the result random (of course, we don't consider the pseudo randomization of the rand () function)?
No, because the upper limit of RAND () is Rand_max, and in general, Rand_max is not an integer multiple of N, then if Rand_max% = R, then the probability of the number between 0--r is larger, and the probability of the number between r+1--n-1 is smaller. And if n > Rand_max, what should I do?
A more suitable scheme is given, which can generate equal probability random number result in any range. Finally, there is a simpler way.
1, if the n<rand_max+1, you want to remove the mantissa,

 R = rand_max-(rand_max+1)%N; Remove Mantissa
  t = rand ();
  while (T > R) t = rand ();
  result = t% N; Random numbers that meet the requirements

2, if the N>rand_max, you can consider segmented sampling, divided into [n/(rnad_max+1)] section, the first and so the probability of getting the paragraph to get each of the elements within each paragraph, such a section also has a similar mantissa problem, not every time just to the integer segment, There must be more or less a remainder segment, how does this part of the value be selected?

Select the remainder segment of the data to choose, first to select the probability of the remainder of the event occurs, and then make a separate selection:

 r = N% (rand_max+1); Remainder
  if ((double) r/n)//probability result
  = N-r+myrandom (r) of the remainder segment;//Myrandom can be implemented with code in case 1
  else
  result = rand () +myrandom (n/(rand_max+1)) * (rand_max+1); If the remainder segment is not selected, then the segment selection

Complete code:

#include <iostream.h> #include <time.h> #include <stdlib.h> const double minprob=1.0/(rand_max+1);
 BOOL happened (double probability)//probability 0~1 {if (probability<=0) {return false;
 } if (Probability<minprob) {return rand () ==0&&happened (probability* (rand_max+1));
 } if (rand () <=probability* (rand_max+1)) {return true;
return false;
 A long myrandom (long N)//produces 0~n-1 equal probability random number {t=0;
 if (N<=rand_max) {long r=rand_max-(rand_max+1)%n;//mantissa t = RAND ();
 while (T > r) {t = rand ();
 return t% n;
 else {Long R = n% (rand_max+1);//Remainder if (happened (double) r/n)//The probability of taking the remainder {return n-r+myrandom (R);
 else {return rand () +myrandom (n/(rand_max+1)) * (rand_max+1);
There is another very simple way of using random_shuffle (Randomaccessiterator _first, Randomaccessiterator _last). For example, to generate a random number between 0--n-1, you can write #include <algorithm> #include <vector> long myrandom (long N) {std::vector<long& Gt VL (N);
 To define a vector with a size of nFor (long i=0 i<n; ++i) {Vl[i] = i;
 } std::random_shuffle (Vl.begin (), Vl.end ());
Return (*vl.begin ()); Random_shuffle also has a three-parameter overloaded version random_shuffle (Randomaccessiterator _first, Randomaccessiterator _last,
 randomnumbergenerator& _rand)

The

Third parameter can accept a custom random number generator to randomize the elements between the first two parameters. The flaw of this method
is that if you just need a random number, the space consumes a lot when n is very large!

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.