-
Dec 20th, 2014, 01:23 AM
#1
Thread Starter
Addicted Member
Cannot Create Random Text From Lists *Solved*
Hello everyone and happy holidays!
I have a question, how does one create random text using Random and Lists? In this example I have a list of vowels and a list of consonants, and depending on the length, it will keep adding random letters to the string, (CName). I am not recieving any new string in LabelName. I've done this example before, and it worked, so now I'm really confused! Help is greatly apprecieated, thanks!
vb Code:
Private Sub Rename()
CName = ""
Dim Length As Int32 = NumericLength.Value
Randomize()
Do Until Length < 1
Length -= 1
Dim R1 As Random = New Random
Dim ChooseLetter1 As Int32 = Math.Round(R1.Next(0, Consonants.Count))
CName += Consonants(ChooseLetter1)
Dim R2 As Random = New Random
Dim ChooseLetter2 As Int32 = Math.Round(R2.Next(0, Vowels.Count))
CName += Vowels(ChooseLetter2)
Loop
LabelName.Text = CName
End Sub
I found my error, I forgot to add in the "add characters to lists" block.
Last edited by NinjaNic; Dec 20th, 2014 at 01:41 PM.
-
Dec 20th, 2014, 02:04 AM
#2
Re: Cannot Create Random Text From Lists
You shouldn't be reinitializing random objects in a loop like that. It will most likely choose the same letter over and over when done in a very short period of time.
Only use one Random object, created only once.
Randomize is not for use with the Random object, so is not needed.
A quick modified test works fine.
Code:
Dim Consonants() As String = {"b", "c", "d", "f", "g", "h", "j", "k", "l", "m", "n", "p", "r", "s", "t", "v", "w"}
Dim Vowels() As String = {"a", "e", "i", "o", "u"}
Dim R1 As New Random
Private Sub Rename()
Dim CName As String
CName = ""
Dim Length As Int32 = NumericLength.Value
Do Until Length < 1
Length -= 1
Dim ChooseLetter1 As Int32 = Math.Round(R1.Next(0, Consonants.Count))
CName += Consonants(ChooseLetter1)
Dim ChooseLetter2 As Int32 = Math.Round(R1.Next(0, Vowels.Count))
CName += Vowels(ChooseLetter2)
Loop
LabelName.Text = CName
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Rename()
End Sub
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|