You are now following this channel
- You will see updates in your content feed.
- You may receive emails, depending on your notification preferences.
You are now following this topic
- You will see updates in your content feed.
- You may receive emails, depending on your notification preferences.
If a large number of fair N-sided dice are rolled, the average of the simulated rolls is likely to be close to the mean of 1,2,...N i.e. the expected value of one die. For example, the expected value of a 6-sided die is 3.5.
Given N, simulate 1e8 N-sided dice rolls by creating a vector of 1e8 uniformly distributed random integers. Return the difference between the mean of this vector and the mean of integers from 1 to N.
function dice_diff = loln(N)
A=randi([1,N],1e8,1)
M=mean(A)
B=1:N
m=mean(B)
dice_diff =abs(M-m);
end
Here is my code, but it can't work out as it needs too long time to creat A.
7 Comments
后面都加分号就可以解决
It works as expected
N = 6;
A = randi([1,N],1e8,1);
M = mean(A);
B = 1:N;
m = mean(B);
dice_diff = abs(M-m);
Result:
>> dice_diff
dice_diff =
1.7416e-04
The difference is 0.000174 which is the expected result. If it take a long time, you can try to use fewer random numbers.
Permanently delete this reply?
This cannot be undone.
Giuseppe Degan Di Dieco
on 27 Aug 2021
(Edited )
Thanks Ameer for the help, easy and straightforward.
Best.
I am glad to be of help.
Thank you, I have been confused by this question for several days.
Your code is correct. However, you didn't add semicolon ';' at the end of each statement. This tells MATLAB to print 1e8 number on the screen. Mathworks allocate a finite time to each solution on Cody. The display takes so long that the Cody server thinks that the is a bug in the code. Just adding a semicolon will correct the issue
function dice_diff = loln(N)
A=randi([1,N],1e8,1);
M=mean(A);
B=1:N;
m=mean(B);
dice_diff = M-m;
end
Thank you for your answer and your patience, but it always returns wrong when I submit to the cody system.
As the requirement of this quesiton, it should be 1e8 times.
Sign in to participate
Posts by this author
-
Discussion
Tags
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)