C#随机函数滚动3个骰子 [英] C# random function to roll 3 dice

查看:118
本文介绍了C#随机函数滚动3个骰子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

当我同时滚动它时,如何启动一个随机生成3个骰子整数的函数?

我的目标是得到3个数字并将它们放在一起(从大到小),例如,如果我滚动3,1,4我需要显示431.我启动代码时不确定如何继续。我为3个骰子创建了3个标签和3个文本框,并且滚动按钮将显示随机结果,但我卡住了,不知道如何继续!

你能不能给我一个推动?

谢谢



我的尝试:



Hello,
How can I start a function that will randomly generate integers for 3 dice when I roll them at the same time?
My aim is to get the 3 numbers and put them beside each other (larger to smaller), for example if I roll 3,1,4 I need to display 431. I started the code by not sure how to continue. I created 3 labels and 3 textboxes for the 3 dice and a "Roll" button that will display random results but I am stuck and don't know how to proceed!
Can you please give me a push?
Thanks

What I have tried:

Random rnd = new Random();
int dice1 = rnd.Next(1,6); // creates a number between 1 and 6 from dice 1
int dice2 = rnd.Next(1,6);   // creates a number between 1 and 6 from dice 2
int dice3 = rnd.Next(1,6);  // creates a number between 1 and 6 from dice 3

推荐答案

首先移动随机实例出方法,并将其放入类级变量:

Start by moving the Random instance out of the method, and putting it into a class level variable:
private Random rnd = new Random();
void MyMethod()
    {
    int dice1 = rnd.Next(1,6); // creates a number between 1 and 6 from dice 1
    int dice2 = rnd.Next(1,6);   // creates a number between 1 and 6 from dice 2
    int dice3 = rnd.Next(1,6);  // creates a number between 1 and 6 from dice 3
    ...
    }

原因是随机使用系统时钟初始化,因此您不想反复创建它们,或者如果您过快地调用方法,则会得到相同的序列。使它成为类级别,意味着它被创建一次,所以你没有得到那么重复。



那么,看看的定义下一个方法,您将看到上限是独占的 - 返回的值永远不会等于它 - 因此您需要添加一个以便在六面模具上获得合理的滚动:

The reason for that is that Random is initialized with the system clock, so you don't want to create them over and over, or you will get the same sequence if you call your method too quickly. Making it class level, means it is created once, so you don't get that repetition.

Then, look at the definition of the Next method, and you will see that the upper bound is exclusive - the value returned will never equal it - so you need to add one in order to get a fair roll on a six sided die:

private Random myDie = new Random();
public int RollStandardDie()
    {
    return myDie.Next(1, 6 + 1);
    }



然后拨打 RollStandardDie 三次以获取您的值,并对它们进行排序。最简单的方法是:


Then call RollStandardDie three times to get your values, and sort them. The easiest way is like this:

private Random myDie = new Random();
public int RollStandardDie()
    {
    return myDie.Next(1, 6 + 1);
    }
private void MyButton_Click(object sender, EventArgs ew)
    {
    int[] dice = new int[3];
    for (int i = 0; i < dice.Length; i++)
        {
        dice[i] = RollStandardDie();
        }
    Array.Sort(dice);

然后只需将您的值输入您的标签或文本框:

Then just feed your values into your Labels or TextBoxes:

label1.Text = dice[0].ToString();
TextBox2.Text = dice[1].ToString();


非常感谢帮助......有没有办法做到这一点不使用数组的最后一位?我知道这可能是一个很长的解决方案,但我只想比较并了解其中的差异。

再次感谢
Thanks a lot for helping... is there a way to do the last bit without using an array? I know it might be a long solution but I just want to compare and learn the difference.
Thanks again


这篇关于C#随机函数滚动3个骰子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆