English 中文(简体)
C++ 问题——谁能帮助?
原标题:C++ Question - Can anyone help?

你会想帮助我解决以下问题吗? 感谢。

Question: The percentage of rolling 3 dices of face 6 in one go can be found out mathematically or simulation. The Monte Carlo method is a computer process to find out the solution of a problem by computer simulation. Write a program that roll three dices, calculate their sum, and find out the probability of rolling each possible outcome.

You are given a skeleton program q1dskeleton.c that generates the statistics of rolling a dice of six sides 10000 times. Modify the program so that it generates the statistics of the sum of rolling three six-sided dices. An example of the program output is given in the following. Note that because of the random nature of dice rolling.

Hint: Call rand() 每 throw一次,每 d一次。

Skeleton:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define SIZE 7

int main() {

  int face, roll, frequency[SIZE] = { 0 };

  srand( time(NULL) );

  for (roll = 1; roll <= 10000; roll++) {
    face = rand() % 6 + 1;
    ++frequency[face];
  }
  printf("%s%12s
", "Face", "Frequency");

  for (face = 1; face <= SIZE - 1; face++)
    printf("%4d%12d
", face, frequency[face]);
  getchar();
}

产出:

Face Frequency
3     49
4     129
5     276
6     481
7     669
8     994
9     1131
10    1213
11    1269
12    1197
13    962
14    707
15    464
16    268
17    144
18    47
最佳回答

Also, when calculating rand() you need to realize it is not all that random as we hope.

From the documentation at: http://www.cplusplus.com/reference/clibrary/cstdlib/rand/ "Notice though that this modulo operation does not generate a truly uniformly distributed random number in the span (since in most cases lower numbers are slightly more likely), but it is generally a good approximation for short spans."

为了取得更好的结果,最好在为大面积地区找到一种不同的公式方面做一些工作,这种模式更有可能产生随机结果。

For instance, for dice one you get a random number between 30 and 90 and the formula is (int)(((float)result - 30) / 10) + 0.5 or something along those lines.

那么,如果是这样的话,你就可以在500到500之间找到一个数字,并拿出另一个公式。

To make your homework assignment more interesting you could have a look at how different online casinos/poker rooms do this. http://www.fulltiltpoker.com/random-number-generator

问题回答

显然,你必须增加<条码>SIZE,因为不再有6种可能性,但18种-Ill 包括不可能达到的总数1和2种,因为你的原始解决办法包括不可能达到的零值。

然后,你不要扔ice一个ice子,而是扔下了 3<>m>,然后添加了价值。 类似(假体编码):

dice1 = first random number
dice2 = second random number
dice3 = third random number
face = dice1 plus dice2 plus dice3

http://em>addition是你用来增加具体阵列内容的。

就奖金点而言,你可能避免印刷不可能的结果。 这非常简单地修改了您的最后<代码>(>说明的起始点。


您所期望的比率为<代码>{1、3、6、10、15、21、25、27、25、21、15、10、6、3、1},因此“理想”产出应合理接近:

Face   Frequency
   3          46
   4         138
   5         277
   6         462
   7         694
   8         972
   9        1157
  10        1250
  11        1250
  12        1157
  13         972
  14         694
  15         462
  16         277
  17         138
  18          46

(even though that only adds up to 9992 samples - it s a crapshoot as to where the other eight show up).





相关问题
How to add/merge several Big O s into one

If I have an algorithm which is comprised of (let s say) three sub-algorithms, all with different O() characteristics, e.g.: algorithm A: O(n) algorithm B: O(log(n)) algorithm C: O(n log(n)) How do ...

Grokking Timsort

There s a (relatively) new sort on the block called Timsort. It s been used as Python s list.sort, and is now going to be the new Array.sort in Java 7. There s some documentation and a tiny Wikipedia ...

Manually implementing high performance algorithms in .NET

As a learning experience I recently tried implementing Quicksort with 3 way partitioning in C#. Apart from needing to add an extra range check on the left/right variables before the recursive call, ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Enumerating All Minimal Directed Cycles Of A Directed Graph

I have a directed graph and my problem is to enumerate all the minimal (cycles that cannot be constructed as the union of other cycles) directed cycles of this graph. This is different from what the ...

Quick padding of a string in Delphi

I was trying to speed up a certain routine in an application, and my profiler, AQTime, identified one method in particular as a bottleneck. The method has been with us for years, and is part of a "...

热门标签