Random 예문, Call Random value
Random random = new Random();
string[] Color = { "yellow", "blue", "red", "green" };
int item = random.Next(Color.Length); // in method
string col = Color[item];
Console.WriteLine("You have " + col );
class Program
{
static Random random = new Random();
public static void Main()
{
Penguin penguin = new Penguin("Winter", "squawk", Penguin.food[GetRandomNumber(Penguin.food.Length)]);
Console.WriteLine(penguin.About());
Console.ReadKey();
}
public static int GetRandomNumber(int range)
{
return random.Next(range);
}
}
class Penguin
{
string Name;
string Noise;
string Food;
public static string[] food = new string[] {"fish", "a", "b", "c", "d", "e" };
public Penguin(string name, string noise, string food)
{
Name = name;
Noise = noise;
Food = food;
}
public string About()
{
return $"The penguin {Name} says {Noise} when given {Food}";
}
}