0

what i'm tryna to create is a program that displays a random description of a person from an array when the form loads.
i tried this but it keeps outputing only one string and thats RAND(1)

Public Class male
Public Property stringpass
Private Sub male_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    Dim RAND(2)
    RAND(0) = "Nobody's perfect of course and Frederick has plenty of less favorable characteristics too." & Environment.NewLine & " His pompous nature and desperation tend to get in the way even at the best of times." & Environment.NewLine & "Fortunately his grace is there to relift spirits when needed."

    RAND(1) = "Animal"
    RAND(2) = "Construct"
    Label2.Text = RAND(rnd)

    Label1.Text = stringpass

End Sub
BlackDante101
  • 81
  • 2
  • 8
  • 1
    What is `rnd` here? (unrelated) `Public Property stringpass` should be `Public Property stringpass As String`. Set `Option Strict ON`, to avoid similar problems. – Jimi Jan 09 '20 at 17:57
  • rnd is supposed to generate some random value from the RAND array i guess – BlackDante101 Jan 09 '20 at 18:00
  • 1
    What it is *supposed* to do is one thing, what it actually is matters. Where and how did you declare this (Field? Local Variable?) – Jimi Jan 09 '20 at 18:03
  • the stringpass variable takes an input from form 1 which is a name then it opens another form with name as label1 and some random description about the name in label2 just like "my name facts app" – BlackDante101 Jan 09 '20 at 18:15
  • You can shuffle the RAND array with https://stackoverflow.com/a/5807238/832052 – djv Jan 09 '20 at 18:28
  • 1
    Or https://stackoverflow.com/a/7258884/832052. You should use `Random` class instead of legacy `RAND` function. – djv Jan 09 '20 at 19:32

1 Answers1

0

Declare the Random class just once at the class level. I used a List(Of T) so you can easily add to the list without changing the dimensions of an array. The .Next method of the Random class returns a number inclusive of the first parameter and exclusive of second parameter.

You may get the strings more than once. Afterall you only have 3 choices.

Public Class male
    Public Property stringpass As String
    Private rnd As New Random

    Private Sub male_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim RAND As New List(Of String)
        RAND.Add("Nobody's perfect of course and Frederick has plenty of less favorable characteristics too." & Environment.NewLine & " His pompous nature and desperation tend to get in the way even at the best of times." & Environment.NewLine & "Fortunately his grace is there to relift spirits when needed.")
        RAND.Add("Animal")
        RAND.Add("Construct")
        Label2.Text = RAND(rnd.Next(0, RAND.Count))
        Label1.Text = stringpass
    End Sub
End Class
Mary
  • 14,926
  • 3
  • 18
  • 27