如何在C#中滚动骰子?

This application uses one instance of the Random() class in the object rnd. It also allocates enough space to hold the totals for scores 3..18 in the array Rolls[]. Member functions OneDice() returns a value between 1 and 6 - rnd.Next(n) returns values in the range 0..n-1, while ThreeDice() calls OneDice() three times. The constructor for the RollDice() clears the Rolls array then calls ThreeDice() however many times (10 million in this case) and increments the appropriate Rolls[] element.

此应用程序在对象rnd中使用Random()类的一个实例。 它还分配了足够的空间来保存数组Rolls []中得分3..18的总数。 成员 函数 OneDice()返回1到6之间的值-rnd.Next(n)返回0..n-1范围内的值,而ThreeDice()调用OneDice() 3次。 RollDice()构造函数清除Rolls数组,然后多次调用ThreeDice() (在这种情况下为1000万),并递增适当的Rolls []元素。

The last part is to print out the generated totals to see that it generates throws in accordance with the probabilities. A 6 sided dice has an average score of 3.5, so three dice should average about 10.5. The totals for 10 and 11 are roughly the same and occur about 12.5% of the time.

最后一部分是打印出生成的总数,以查看它根据概率生成了抛出。 6面骰子的平均得分为3.5,因此3个骰子的平均得分约为10.5。 10和11的总数大致相同,大约占时间的12.5%。

Here is the output of a typical run. It takes no more than a second. Because it's a console application, I included a

这是典型运行的输出。 只需要一秒钟。 因为它是一个控制台应用程序,所以我包含了一个

 Console.ReadKey();

To wait until you hit a key before closing.

等待直到您敲关键再关闭。

节目输出 ( Program Output )

 3 46665
4 138772
5 277440
6 463142
7 693788
8 971653
9 1157160
10 1249360
11 1249908
12 1159074
13 972273
14 695286
15 463270
16 277137
17 138633
18 46439

程序清单 ( Program Listing )

 using System;
using System.Collections.Generic;
using System.Text;
namespace exrand
{
    public class RollDice
    {
        private Random rnd= new Random() ;
        private int[] Rolls = new int[19]; // Holds 3 to 18
        public int OneDice() {
            return rnd.Next(6)+1;
        }
        public int ThreeDice()
        {
            return OneDice() + OneDice() + OneDice() ;
        }
        public RollDice(int Count)
        {
            int i = 0;
            for (i=3;i

翻译自: https://www.thoughtco.com/how-do-i-roll-dice-in-c-958248

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值