SIGN UP MEMBER LOGIN:    
ARTICLE

Generating Random Number in the Array

Posted by Bilal hawaja Articles | C# Language December 17, 2004
This article shows how to generate random number in an array using C#.
Reader Level:

This is for beginners. I've written this Game in C#. It's generating a random number in the array. So when the user enters the number, it check for that number in the index 0 of the array since it's generating only 1 value. So length of array is 1.

using System;
public class B
{
Random r =
new Random();
public string st;
Public int lukynum;
public virtual void PlayGame()
{
int [] scores = {r.Next(1,7)};
Console.Write("Type start to play the game: " );
st = Console.ReadLine();
try
{
if (st == "start")
{
do
{
Console.Write("Please Enter your lucky Number or -1 to Exit: ");
lukynum = Convert.ToInt16(Console.ReadLine());
Console.WriteLine();
if (lukynum == -1)
{
break;
}
for ( int i=0; i<scores.Length; i++)
{
if (scores[i] == lukynum)
{
Console.WriteLine("You won by chooing : " + scores[i]);
Console.WriteLine();
break ;
}
else
{
Console.WriteLine("\n\t****PLEASE TRY AGAIN!****");
Console.WriteLine("Lucky Number is between 1 and 7 ;) \n");
}
}
}
while (lukynum!=-1);
}
else
{
PlayGame();
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
public class C : B
{
public C()
{
base .PlayGame();
}
}
public class MyClient
{
public static void Main()
{
C c =
new C();
}
}

Login to add your contents and source code to this article
share this article :
post comment
 

i want the above program output

Posted by mohaseena Aug 12, 2010
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Nevron Gauge for SharePoint
Become a Sponsor