# Random Dices

This is just an answer to a question that came up in a discussion. Can you simulate 2 dice throws and display the frequencies in EMT?

Actually, this is quite simple. We demonstrate it with 20 repetitions first. The trick in a matrix language is always to do all random stuff beforehand, and then to apply matrix functions to get the desired statistics. In our case sum(W)‘ does the job. It sums all rows of W and returns a vector with of sums. Then getmultiplicities() extracts the statistics. In our case, we know that the numbers 2 to 12 can appear in the sum. If we don’t know that, sort(unique()) extracts a sorted set of unique elements in the data.

>n=20;
>W=intrandom(n,2,6)
6             2
4             1
5             3
1             3
5             6
3             3
1             2
6             4
5             6
3             2
1             3
5             4
6             3
3             3
6             1
3             3
6             2
6             6
4             4
5             3
>sum(W)'
[8,  5,  8,  4,  11,  6,  3,  10,  11,  5,  4,  9,  9,  6,  7,  6,  8,
12,  8,  8]
>f=getmultiplicities(2:12,sum(W)')
[0,  1,  2,  2,  3,  1,  5,  2,  1,  2,  1]


We change n to 20000, and see the expected distribution.

>columnsplot(f,lab=2:12):


Diese Website verwendet Akismet, um Spam zu reduzieren. Erfahre mehr darüber, wie deine Kommentardaten verarbeitet werden.