简体   繁体   中英

random number with the gaussian distribution in an interval matlab

How can one create an integer random number with Normal distribution in an interval in Matlab? Could anyone provide an answer?

I know how to create a random number ,say y, with Normal distribution:

std = 5;
mean = 500;
y = std.*randn + mean;

But it is not an integer number and also it is not in a specific interval

Choose the number from a binomial(N, 0.5) distribution for large N. This will yield something that is as close as you might be able to get to a "normal distribution of integers". The mean will be N/2 and the std deviation N/4. Subtract N/2 to center it about 0.

Say N = 100. Then to generate a sample, you could do:

k = sum(randi(2, [100,1]) - 1);

or:

k = sum(rand(100,1) < 0.5);

You could use randn and convert to integer by rounding the output number. Repeat until the number is in range [a,b] you are interested in. It will likely work fine for wide enough range around the middle, but you will be doing tons of attempts when you want to look at a narrow part of the tail.

Other option is to get any integer from whatever range with equal probability and convert that to gaussian-like in your range. Say numbers 0->10 would become a, 11-50 would be a+1 ... maxint-10:maxint would be b.

If you want integers, you can use randn and round the numbers. However, your second question is kind of weird.

Normal distribution does not have a definite interval. You can only define a "confidence interval" around the mean. For example, 99.7% of the distribution is contained within 3 standard deviations from the mean. But it does not mean that you have a strict interval, it means probability of seeing a number beyond 3xStandard deviations is just too low. Let's say I generated 10000 numbers with mean=100 and std.deviation=10 and rounded them. Then I expect to see numbers between 70 and 130. There might be numbers beyond this interval, but their frequencies(~probabilities) will be low.

mu=100; sigma=10; figure,hist(round(normrnd(mu,sigma,10000,1)),100)

在此输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM