Friday, October 9, 2009

Generating Random Number in C#


//form level declaration
Random random = new Random();
-----------------------------------------------------------------

private void btnRandom1_Click(object sender, EventArgs e)
{    //returns a random number
   int num1 = random.Next();
   this.txtRandom1.Text = num1.ToString();
}
-----------------------------------------------------------------
private void btnRandom2_Click(object sender, EventArgs e)
{   //returns a number less than 1000
   int num2 = random.Next(1000);
   this.txtRandom2.Text = num2.ToString();
}
-----------------------------------------------------------------
private void btnRandom3_Click(object sender, EventArgs e)
{   //returns a number between 1 to 10
   int num3 = random.Next(1,10);
   this.txtRandom3.Text = num3.ToString();
}
-----------------------------------------------------------------
*** end of program ***

No comments:

Post a Comment