Results 1 to 2 of 2

Thread: Cannot Create Random Text From Lists *Solved*

  1. #1

    Thread Starter
    Addicted Member NinjaNic's Avatar
    Join Date
    Dec 2013

    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:
    1. Private Sub Rename()
    3.         CName = ""
    4.         Dim Length As Int32 = NumericLength.Value
    5.         Randomize()
    6.         Do Until Length < 1
    7.             Length -= 1
    8.             Dim R1 As Random = New Random
    9.             Dim ChooseLetter1 As Int32 = Math.Round(R1.Next(0, Consonants.Count))
    10.             CName += Consonants(ChooseLetter1)
    11.             Dim R2 As Random = New Random
    12.             Dim ChooseLetter2 As Int32 = Math.Round(R2.Next(0, Vowels.Count))
    13.             CName += Vowels(ChooseLetter2)
    14.         Loop
    16.         LabelName.Text = CName
    18.     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.

  2. #2
    Sinecure devotee
    Join Date
    Aug 2013
    Southern Tier NY

    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.
      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)
        LabelName.Text = CName
      End Sub
      Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
      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

Click Here to Expand Forum to Full Width