10 Random Numbers (C#)

Started by
9 comments, last by rob64464 20 years, 7 months ago
This is what I want to do basically create a slot machine. I have writen what operations I want to do. Would anybody be so kind to translate this to the c# language? Generate 5 random numbers if number = 1 then print on screen "bar" if number = 2 then print on screen "apple" if number = 3 then print on screen "cherry" if number = 4 then print on screen "grapes" if number = 5 then print on screen "lemon" Thanks again, Robert
Advertisement
Random rnd = new Random();
for (int i=0; i<5; i++) {

int number = rnd.Next(5) + 1;
switch (number) {
case 1:
Console.WriteLine("bar");
break;
case 2:
Console.WriteLine("apple");
break;
case 3:
Console.WriteLine("cherry");
break;
case 4:
Console.WriteLine("grapes");
break;
case 5:
Console.WriteLine("lemon");
break;

}
}
Thank you this really has helped me :-)

Rob
string[] slots = new string[]{ "bar", "apple", "cherry", "grapes", "lemon" };Random rnd = new Random();for( int i = 0; i < 5; i++ )   Console.WriteLine( slots[ rnd.Next(5) ] )


--
AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.
[Project site] [Blog] [RSS] [Browse the source] [IRC channel]
--AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.[Project site] [IRC channel] [Blog]
How would i add a basic add up code?

[STAThread]
static void Main(string[] args)
{
Random rnd = new Random();
for (int i=0; i<3; i++) // going to print a total of 3 items
{

int number = rnd.Next(5) + 1; //go through all 5 cases
switch (number)
{
case 1: // if the number = case 1 then print ''BAR''
Console.WriteLine("BAR");
break;
case 2: // if the number = case 2 then print ''APPPLE''
Console.WriteLine("APPLE");
break;
case 3: // if the number = case 3 then print ''CHERRY''
Console.WriteLine("CHERRY");
break;
case 4: // if the number = case 4 then print ''GRAPES''
Console.WriteLine("GRAPES");
break;
case 5: // if the number = case 5 then print ''LEMON''
Console.WriteLine("LEMON");
break;

}
}

{
// Check to see if the user has won anything!


}

thank you,
rob
"How would i add a basic add up code?"

What do you mean?

"int number = rnd.Next(5) + 1;"

int number = rnd.Next(1, 6);
I mean if you get:
3 cherrys in a row add 20
and so on. Just as a basic example.

Thanks,
Rob
This is c code, i suppose it also works in c# as is:

const int cherry = 4; //or some other number :)if ((slots[0]==cherry) && (slots[1]==cherry) && (slots[2]==cherry))Score += 20; 




[edited by - Nik02 on September 25, 2003 9:39:42 AM]

Niko Suni

is this a homework question from your first c# class?
Meh, best way:

enum Slots {Bar, Cherry, Apple, Grapes, Lemon};
Random rand = new Random();

for (int i = 0; i < 5; i++)
Console.Out.WriteLine("{0}",(Slots)rand.Next(4));

This topic is closed to new replies.

Advertisement