[英]How do I make 3 random numbers in C# as a row of 3 in an array and assign each to 3 variables?
I'm trying to generate 3 random numbers and output it onto a label as an array with 3 in a row. 我正在尝试生成3个随机数并将其输出到标签上,作为连续3个数组。 I am just unsure about doing so.
我只是不确定这样做。 So far, I initialized my random value as
到目前为止,我将我的随机值初始化为
Random valueRandom = new Random();
private void Main()
{
for (int i = 1; i <= gameAmountInteger; i++)
DoNextNumber(valueRandom);
}
private void DoNextNumber(Random valueRandom)
{
int int1;
int1 = valueRandom.Next(0, 1);
displayLabel.Text = valueRandom.ToString();
}
It should be (range of 0 - 10) 3, 5, 2
它应该是(0-10的范围)
3, 5, 2
Few things you are doing wrongly: 您做错的一些事情:
valueRandom.Next(0, 1);
valueRandom.Next(0,1);
Here the first value of the .Next()
method indicates the lower bound and the second parameter indicates the upper bound, and you wanted the upper bound as 10
but given as 1
. 在此
.Next()
方法的第一个值指示下限,第二个参数指示上限,您希望将上限设置为10
但给定为1
。 As you wanted to get the random number between 0-10
you have to give as valueRandom.Next(0, 10)
, which will not include 10
if need to include 10
means have to give it as valueRandom.Next(0, 11)
. 当您想要获得
0-10
之间的随机数时,您必须将其作为valueRandom.Next(0, 10)
,如果需要包含10
意味着必须将其作为valueRandom.Next(0, 11)
valueRandom.Next(0, 10)
将不包括10
。
displayLabel.Text = valueRandom.ToString();
displayLabel.Text = valueRandom.ToString();
From the DoNextNumber
method you are assigning the generated random number to the required UI element, so you will get the last number only in the UI. 通过
DoNextNumber
方法,您将生成的随机数分配给所需的UI元素,因此您将仅获得UI中的最后一个数字。 If you need to get all the numbers displayed means you have to give it as displayLabel.Text = displayLabel.Text + valueRandom.ToString();
如果需要获取所有显示的数字,则意味着必须将其指定为
displayLabel.Text = displayLabel.Text + valueRandom.ToString();
Consider duplicates
考虑重复
There are possibilities of getting duplicate numbers while executing the .Next()
repeatedly, if duplicates will be an issue in your scenario means you have to keep the outcomes into a collection and check for existence before pushing the newly generated random number to the collection. 重复执行
.Next()
,有可能会得到重复的数字,如果在您的方案中重复将是一个问题,则意味着您必须将结果保留在一个集合中并检查是否存在,然后才能将新生成的随机数推入该集合。 In this case write the value to the UI after getting the required collection. 在这种情况下,在获取所需的集合后将值写入UI。 In that case you can use the code as like this:
displayLabel.Text = String.Join(",", randomCollection);
在这种情况下,您可以使用如下代码:
displayLabel.Text = String.Join(",", randomCollection);
where randomCollection
is the collection which I mentioned above 其中
randomCollection
是我上面提到的集合
You should be using valueRandom.Next(0, 10)
, and I would refactor your code like following: 您应该使用
valueRandom.Next(0, 10)
,我将重构代码,如下所示:
public static void Main()
{
displayLabel.Text = GetRandomsAsString(gameAmountInteger);
}
private string GetRandomsAsString(int numberOfrandoms)
{
var valueRandom = new Random();
var randoms = "";
for (int i = 0; i < numberOfrandoms; i++)
{
randoms += valueRandom.Next(0, 10);
}
return randoms;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.