Generating method of random number with arbitrary distribution-VC Program Realization method

Source: Internet
Author: User
Tags min rand range

Summary:

Random number in the actual use of very much, such as game design, signal processing, usually we can easily get the average distribution of random numbers. But how do we generate random numbers of other distributions based on the average distribution of random numbers? In this paper, a method based on the geometrical visual area is presented, and the generation of random numbers is discussed with the example of normal distribution.

Body:

Generation of the average distributed random number

As we all know, random numbers in all aspects have a great role in the VC environment, for us to provide the library function rand () to produce a random integer. The random number is averaged evenly between 0~rand_max, and Rand_max is a constant, defined in the VC6.0 environment:

#define RAND_MAX 0X7FFF

It is the maximum value of a short data, and if you want to produce a random number of floating-point types, you can get rand ()/1000.0 to obtain a random floating point that is evenly distributed between 0~32.767. If you want to make the range larger, you can achieve the average distribution of random numbers in any range by generating a linear combination of several random numbers. For example, a random number that produces an average distribution of four-bit precision between -1000~1000 can be implemented in this way. First, a random integer from 0 to 10000 is generated. The method is as follows:

int a = rand ()%10000;

The four-bit decimal is then retained to produce random decimals between 0~1:

Double b = (double) a/10000.0;

Then the random number in any range can be produced by linear combination, and the random number of the average distribution within the -1000~1000 can be achieved:

Double Dvalue = (rand ()%10000)/10000.0*1000-(rand ()%10000)/10000.0*1000;

Then the dvalue is the value you want.

So far, you may think that all the work has been done, but in fact, if you look carefully, you will find that there is a problem, and the above simplification becomes:

Double Dvalue = (rand ()%10000)/10.0-(rand ()%10000)/10.0;

In this way, the resulting random number range is correct, but the accuracy is not correct, has become only a correct number of decimal random numbers, the back three decimal places are zero, obviously not what we ask, what reason, and how to do it.

First look for the reason, Rand () produced a random number resolution of 32767, two is 65534, and after the resolution has to be reduced to 10000, two is 20000 and the resolution required for 1000*10000*2=20000000, obviously far from enough. The following methods provide the correct results:

double a = (rand()%10000) * (rand()%1000)/10000.0;
double b = (rand()%10000) * (rand()%1000)/10000.0;
double dValue = a-b;

Then the dvalue is the desired result. In the following function you can achieve a random number that produces an average distribution within an interval, with a precision of 4 decimal places.

double AverageRandom(double min,double max)
{
int minInteger = (int)(min*10000);
int maxInteger = (int)(max*10000);
int randInteger = rand()*rand();
int diffInteger = maxInteger - minInteger;
int resultInteger = randInteger % diffInteger + minInteger;
return resultInteger/10000.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.