Tutorial #1 – Random Function in C#
by Samet Kilictas on Nov.09, 2008, under C#, Programming
Since there are some other ways as well. Here is my little function which can generate a random integer
private int RandomNumber(int min, int max)
{
Random random = new Random();
int rand = random.Next(min, max);
return rand;
}
I need to convert this interger number to a string. And this function takes to parameters minimum & maximum. That helps you to determine the interval of this random numbers.
//Determine about operator
String[] operators = { "+", "-", "/", "*" };
int rand3 = random.Next(1, 5);
//Console.WriteLine(rand3.ToString());
switch (rand3.ToString())
{
case "1":
label1.Text = "+";
total = rand1 + rand2;
break;
case "2":
label1.Text = "-";
total = rand1 - rand2;
break;
case "3":
label1.Text = "/";
total = rand1 / rand2;
break;
case "4":
label1.Text = "*";
total = rand1 * rand2;
break;
default:
label1.Text = "Error";
break;
}
Here i put a switch as an example. It gives me a random number and then i am able to get a random char from my string array.
Thanks for dropping by! Feel free to join the discussion by leaving comments, and stay updated by subscribing to the 