Functions in C#

Random numbers are useful in many situations, not just games, but also in serious applications such as simulations, educational software and others.

In C# you need to declare the random number seed before you can use it to generate the random values.

You should do this just once to avoid lots of repetition.

public partial class Form1 : Form
{ 
Random random = new Random(); //declare random just once
...
 
private void button1_Click(object sender, EventArgs e)
 {
   int randomNumber = random.Next(0, 100); //0..99 inclusive
   this.Text = "" + randomNumber; 
   //use the + to concatentate, you can't write a number to a string property
} 

We will use random values in the drawing lesson to generate random colors.

NEXT: Circle: Write a function to find area