Samet Kilictas’s Blog

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.

VN:F [1.0.6_327]
Rating: 0.0/10 (0 votes cast)
:, ,
No comments for this entry yet...

Leave a Reply

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Visit our friends!

A few highly recommended friends...