The problem __c++ of C + + large random number

Source: Internet
Author: User
Tags rand

The maximum number of random numbers in C + + is Rand_max, defined in header file <stdlib.h>.

Under Windows platform vs is 0x7fff, the Xcode of the MacBook is the maximum value for int. It seems that different platforms are different.

But Windows is too damn small.

First, the usual random function:

int littlerand (int min, int max)
{
	//Considering that Rand_max may not be equal to 0x7fff under different platforms, you cannot put Rand_max*rand_max in order to avoid int exploding

	if (min > Max)
	{
		min = max;
	}
	 
	int randv = rand ()% (max-min + 1);

	int randresult = min + randv;
	return randresult;
}
Want to optimize: think of two multiplication, the maximum value is 100 million, and are random, should be equal.
int bigrand (int min, int max)
{
	//Considering that Rand_max may not be equal to 0x7fff under different platforms, you cannot put Rand_max*rand_max in order to avoid int exploding
 
	if (min > Max)
	{
		min = max;
	}

	int rand1 = rand ()% 10000;
	int rand2 = rand ()% 10000;
	int RANDV = (RAND1 * rand2)% (max-min + 1);

	int randresult = min + randv; 
	return randresult;
}
So I ran 1 million times to generate a random number within 10, counting the number of occurrences.
int main ()
{ 
	srand (time (0));
	Std::map<int, int> A;
	Std::map<int, int> b;
	for (int i = 0; i < 1000000 i + +)
	{
		A[bigrand (1,)]++;
		B[littlerand (1)]++;
	}
	for (int i = 1; I <= + + i)
	{
		cout << i << ": \ T" << A[i] << "T" << B[i] &l t;< Endl;
	}

	return 0;
}
Results:

The left is the number of large random numbers, the right is the original.

It can be seen that the right side of the more normal, basically is the average.

And on the left, it's obviously a little bit bigger. Equal to 1 is particularly high. As for how to produce I also think impassability.

But you can imagine narrowing the scope. are randomly 1 and 2.

The probabilities of 1 and 2 are all 0.5, and two are multiplied by 4. We can get a random number in the range of 1-4.

But the probability is obviously not equal. If performed two times, both 1 and 2 appear

Can wait until 1:1 times, 2:2 times, 3:0 times, 4:1 times.

Obviously, the multiplication is definitely different.

Come and see the multiplication formula that my mother called me as a child.

    Std::map<int, int> R;
    for (int i = 0; i < ++i) {for
        (int j = 0; J < ++j) {
            r[i * j%10]++;
        }
    }
    for (int i = 0; i < ++i) {
        cout << i << "T" << R[i] << Endl;
    }
Results:

0

1 4

2

3 4

4

5 9

6

7 4

8

9 4 that should be how to do it, in fact, should use the displacement method. The number you calculate is 10, every time you move 1 to the left, it's multiplied by 10, like the code above me,

int bigrand (int min, int max)
{  
	if (min > Max)
	{
		min = max;
	}

	int rand1 = rand ()% 10000;
	int rand2 = rand ()% 10000;
	int RANDV = (RAND1 * 10000 + rand2)% (max-min + 1);//change to such

	int randresult = min + randv; 
	return randresult;
}

10000 below and 10000 are completely irrelevant, two random irrelevant. Only in this way can we make the right random.

In fact, the model is the time, the number of patterns, the first random will be multiplied by how much. Rand1 is multiplied by the Rand2 's modulus.
Baidu a random number of others.

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
unsigned long ulrand (void) { Return (((
     unsigned long) rand () <<24) &0xff000000ul)
    | ( ((unsigned long) rand () <<12) &0x00fff000ul)
    | ( ((unsigned long) rand ()    ) &0x00000ffful));
}
int i;
unsigned long ul;
void Main () {
    Srand (Time (NULL));
    for (i=0;i<10;i++) {
        ul=ulrand ();
        printf ("%010lu 0x%08x\n", Ul,ul);
    }
And there's this simple point:

int Bigrand ()
{return
    Rand_max*rand () + RAND (); 
}
In fact, by shifting the way out of that random number of the maximum value on the line.

But one problem to consider is the cross-platform problem. The lengths of the different platforms may be different, and the Rand_max may be different. Always shift, the array may appear out of bounds.

So to be on the safe side, I'm writing two random numbers of 10000 max values.





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.