ARTICLE

Generating Random Number in the Array using VB.NET

Posted by Bilal hawaja Articles | Visual Basic 2010 December 17, 2004
This article shows how to generate random number in an array using VB.
 
Reader Level:

This is for beginners. I've written this Game in VB.NET, 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.

Public Class B
Private r As Random = New
Random
Public st As
String
Public lukynum As
Integer

Public
Overridable Sub PlayGame()
Dim scores As Integer
() = {r.Next(1, 7)}
Console.Write("Type start to play the game: ")
st = Console.ReadLine()
Try
If st = "start"
Then
Do
Console.Write("Please Enter your lucky Number or -1 to Exit: ")
lukynum = Convert.ToInt16(Console.ReadLine())
Console.WriteLine()
If lukynum = -1
Then
Exit
Do
End
If
Dim i As Integer
= 0
Do While
i < scores.Length
If scores(i) = lukynum
Then
Console.WriteLine("You won by chooing : " & scores(i))
Console.WriteLine()
Exit
Do
Else
Console.WriteLine(Constants.vbLf + Constants.vbTab & "****PLEASE TRY AGAIN!****")
Console.WriteLine("Lucky Number is between 1 and 7 ;) " & Constants.vbLf)
End
If
i += 1
Loop
Loop While
lukynum <> -1
Else
PlayGame()
End
If
Catch ex As
Exception
Console.WriteLine(ex.Message)
End
Try
End
Sub

End
Class

Public Class C : Inherits B

Public Sub New()
MyBase
.PlayGame()
End
Sub

End Class

Public Class MyClient
Public Shared Sub
Main()
Dim c As C = New
C
End
Sub

End Class

NOTE: THIS ARTICLE IS CONVERTED FROM C# TO VB.NET USING A CONVERSION TOOL. ORIGINAL ARTICLE CAN BE FOUND ON C# CORNER (WWW.C-SHARPCORNER.COM).

share this article :
post comment
 

Hi, I am new on this website and quite new on programming too. Why isn´t there any picture on the program so you know the best way to draw the buttons and textbox etc?

 

Posted by Bjorn Jun 22, 2006
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Become a Sponsor