Random numbers in matlab

조회 수: 1 (최근 30일)
Peter
Peter 2012년 3월 9일
Hi, I'm trying to do a simple simulation: Take a total of N objects and partition them into two bins, with probability of going into each bin = 1/2. To do this, I generate a random number, using rand(1), a total of N times. If the random number is less than 0.5, I "put" the object in 1 bin, while if it is greater than 1/2, I put the object in another bin. The average number of objects in each bin should be N/2. Also, one can show that the average of the difference of the number of objects in the two bins is Sqrt(N). I wrote some simple code to this simulation, and I indeed see that the average number of objects in a single bin approaches N/2 very closely. However, when I look at the average of the difference of the number of objects, I get something slightly less than Sqrt(N). Is rand not accurate enough to capture the higher moments of the distribution accurately.

답변 (1개)

Tom Lane
Tom Lane 2012년 3월 9일
Are you sure of the sqrt(N) result? I believe you would expect this if you took the square root of the average squared distance between b, the number in the first bin, and 10-b, the number in the second bin. But I haven't worked out the theoretical value for the average distance.
>> b = binornd(10,.5,1e6,1);
>> sqrt(mean((b-(10-b)).^2))
ans =
3.1605
>> mean(abs(b-(10-b)))
ans =
2.4583
>> sqrt(10)
ans =
3.1623

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by