Click here to Skip to main content
15,909,445 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
I'm trying generate 100 random numbers in a listbox. How can I do that with my code?

C#
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);
            
        }
Posted
Updated 10-May-14 5:10am
v2
Comments
[no name] 10-May-14 12:04pm    
If you really need something which "earn the Name random", take GUID!

1 solution

Any effort?

You can generate 100 numbers between 1 and 1000 like this:

C#
var randomNumbers = new List<int>();
var random = new Random(0);
for (var count = 0; count < 100; count++)
{
    randomNumbers.Add(random.Next(1, 1000));
}


Or use the same list/logic to bind it to a UI element.
 
Share this answer
 
Comments
Member 10807798 10-May-14 11:38am    
If i change my for (int i = 0; i < die.Length; i++) to for (int i = 0; i < 100; i++), I get an error.
Manas Bhardwaj 10-May-14 11:46am    
What's the error?
Member 10807798 10-May-14 11:48am    
It highlighted

dices[i].Image = this.imageList1.Images[imageNumber];

The error said ArgumentOutOfRangeException was unhandle
Manas Bhardwaj 10-May-14 11:57am    
That explains a lot. It's clear that you don't have that many (100) results in your array and you are trying to get results for them.
Member 10807798 10-May-14 12:13pm    
Alright, my array is die = new int[2] { 0, 0 };. How can i change it to give a 100 result?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900