getrandomnumber

mmarkym48

New member
Joined
Aug 22, 2005
Messages
1
Programming Experience
1-3
Get Random Number

Hi- I have this code to generate randomly the alphabet, the letters R and L, and the numbers 3,4 ,5. You'll see that I concatenate the 3 4 and 5 with the R and L and use the letter from the alphabet alone. So I have 2 labels- one holds the alphabet letter and the other holds both the R or L and the 3,4 or 5.

The problem here is that I am not generating 3L or 5R.

Then I have a select case structure that is self explanatory but although it was working nothing is happening now.

mark
PublicClass Form1

Dim LETTER AsString

Dim checkWord AsString

Dim DIE AsString

PrivateFunction getrandomchar(ByVal low AsInteger, ByVal high AsInteger) AsInteger

getrandomchar = New System.Random().Next(low, high)

EndFunction

PrivateFunction getrandomdie(ByVal low AsInteger, ByVal high AsInteger) AsInteger

getrandomdie = New System.Random().Next(low, high)

EndFunction



PrivateFunction getrandomNumber(ByVal low AsInteger, ByVal high AsInteger) AsInteger

getrandomNumber = New System.Random().Next(low, high)



EndFunction



PrivateSub btnChooseletter_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnChooseletter.Click

Dim myAlphabet AsString = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"

Dim myRandomLetter AsChar = myAlphabet.Substring(getrandomNumber(0, 26), 1)

lblCharac.Text = myRandomLetter

EndSub

PrivateSub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) HandlesMyBase.Load

Randomize()

EndSub

PrivateSub btnDie_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDie.Click

Dim myDieNum AsString = "345"

Dim myDieChar AsString = "RL"

Dim myRandomdieNum AsChar = myDieNum.Substring(getrandomdie(0, 3), 1)

Dim myRandomdieChar AsChar = myDieChar.Substring(getrandomchar(0, 2), 1)

lblDie.Text = myRandomdieNum & myRandomdieChar

EndSub

PrivateSub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim LETTER AsString

Dim checkWord AsString

Dim DIE AsString

Dim wordLength AsInteger

LETTER = lblCharac.Text

DIE = lblDie.Text

checkWord = txtEnterWord.Text

wordLength = checkWord.Length



If checkWord.Length < 3 Then

MsgBox("Please use words that are 4 or more characters in length")

txtEnterWord.Text = ""

EndIf

SelectCase DIE

Case "3L"

If checkWord.Substring(2, 1) = LETTER Then

MsgBox("CORRECT")

lblScore.Text = lblScore.Text + checkWord.Length

checkWord = ""

EndIf

Case "4L"

If checkWord.Substring(3, 1) = LETTER Then

MsgBox("CORRECT")

lblScore.Text = lblScore.Text + checkWord.Length

checkWord = ""

EndIf

Case "5L"

If checkWord.Substring(4, 1) = LETTER Then

MsgBox("CORRECT")

lblScore.Text = lblScore.Text + checkWord.Length

checkWord = ""

EndIf

Case "3R"





EndSelect



EndSub

 
Last edited by a moderator:
MMARKYM The IP Address is: 141.154.75.23. The host name is: pool-141-154-75-23.bos.east.verizon.net.

MMARKYM48 The IP Address is: 141.154.75.23. The host name is: pool-141-154-75-23.bos.east.verizon.net.

You have registered twice ... please don't do that again. Feel free to ask more under same nick!!! If you didn't note we are always willing to help people that need a help.

Cheers ;)
 
Do not create a new Random each time you want to generate a random number. You should create a single Random object and then call the desired methods when you need a random number. Also, here's a better GetRandomChar method:
VB.NET:
	'Seed the random number generator based on the current time to create a different psuedo-random sequence each time.
	Private rand As New Random(CInt(Date.Now.Ticks Mod Integer.MaxValue))

	Private Function GetRandomChar() As Char
		'Get a random ASCII code from A to Z and convert it to the corresponding character.
		Return Convert.ToChar(Me.rand.Next(Convert.ToInt32("A"c), Convert.ToInt32("Z"c) + 1))
	End Function
 
Back
Top