如何生成100个随机数 [英] How Do I Generate 100 Random Numbers
本文介绍了如何生成100个随机数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试在列表框中生成100个随机数。我怎么能用我的代码呢?
I'm trying generate 100 random numbers in a listbox. How can I do that with my code?
public partial class Form1 : Form
{
#region Declaration
int[] die;
int Total;
int[] i_Array = new int[5];
Random rand;
List<int> diceresults = new List<int>();
List<picturebox> dices= new List<picturebox>();
BindingList<object> listBox = new BindingList<object>();
MyResult MR = new MyResult();
#endregion
#region Initialization
public Form1()
{
InitializeComponent();
dices.Add(picbxDie1);
dices.Add(picbxDie2);
}
private void Form1_Load(object sender, EventArgs e)
{
die = new int[2] { 0, 0 };
rand = new Random();
}
#endregion
#region Private Methods
private void btnWrite_Click(object sender, EventArgs e)
{
rolldice();
}
private void rolldice()
{
Total = 0;
diceresults.Clear();
for (int i = 1; i < die.Length; i++)
{
diceresults.Add(rand.Next(1, 7))
int imageNumber = diceresults[i] - 1 ;
dices[i].Image = this.imageList1.Images[imageNumber];With Value
Total += diceresults[i];
txtResult.Text = Total.ToString();
}
this.txtResult.Focus();
listBox1.Items.Add("Roll A Sum of " + txtResult.Text);
}
推荐答案
任何努力?
您可以生成1到1000之间的100个数字,如下所示:
Any effort?
You can generate 100 numbers between 1 and 1000 like this:
var randomNumbers = new List<int>();
var random = new Random(0);
for (var count = 0; count < 100; count++)
{
randomNumbers.Add(random.Next(1, 1000));
}
或者使用相同的列表/逻辑将其绑定到UI元素。
Or use the same list/logic to bind it to a UI element.
这篇关于如何生成100个随机数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文