Random Generator Class in C#


The C Sharp language has a good support for creating random entities such as integers, bytes or doubles. First we need to create the Random Object. The next step is to call the Next() function in which we may supply a minimum and maximum value for the random integer. In our case we have set the minimum to 1 and maximum to 9. So, each time we click the button we have a set of random values in the three labels. Next, we check for the equivalence of the three values; and if they are then we append two zeroes to the text value of the label thereby increasing the value 100 times. Finally we use the MessageBox() to show the amount of money won.

         


using System;

namespace Playing_with_the_Random_Class
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        Random rd = new Random();

        private void button1_Click(object sender, EventArgs e)
        {
            label1.Text = Convert.ToString(rd.Next(1, 9));
            label2.Text = Convert.ToString(rd.Next(1, 9));
            label3.Text = Convert.ToString(rd.Next(1, 9));

            if (label1.Text == label2.Text && label1.Text == label3.Text)
            {
                MessageBox.Show("U HAVE WON " + "$ " + label1.Text+"00"+ " !!");
            }
            else
            {
                MessageBox.Show("Better Luck Next Time");
            }
        }
      
    }
}




Please Note :
** Do not Copy & Paste code written here ; instead type it in your Development Environment
** Testing done in .Net Framework 4.5 but code should be very similar for previous versions of .Net
** All Program Codes here are  100%  tested & running.