0

I am creating a program that acts as a question ball. The user will ask a question and then the program will come up with random response. I am struggling with how to get the program to randomly generate a response out of the array I have made. I don't know how to do the string array and make it randomized. I have tried many things but none have worked so here is my basic code.

Sub Main()

Dim userquestion As String   
Dim questions(0 To 9) As String
Console.WriteLine("Simply enter a question and it will be answered.")
userquestion = Console.ReadLine()

questions(0) = "Most Likely."
questions(1) = "Outlook Good"
questions(2) = "Yes"
questions(3) = "Sorry, I'm confused ask again later "
questions(4) = "I can't predict now"
questions(5) = "Try to concentrate more so I can use to answer the question."
questions(6) = "Don't COunt on it"
questions(7) = "Yes Do it it will work !"
questions(8) = "My reply is no"
questions(9) = "Sorry, I was sleeping I forgot the question"

Console.WriteLine("The answer is ")

Console.WriteLine("Did you like the answer ? ")
Console.ReadLine()
1
  • Dim questions(0 To 9) As String --> Dim questions(9) As String Commented Oct 7, 2015 at 16:00

1 Answer 1

1

You need to use a Random object. Generate the random number and then use that random number as an index for your array when you display it. Something like:

Dim randNumGenerator As New Random()
Dim randNum As Integer

randNum = randNumGenerator.Next(0, 10) 'Generates a random number from 0 to 9 (the array indexes)

Console.WriteLine(questions(randNum))
Sign up to request clarification or add additional context in comments.

4 Comments

best practise is that randNumGenerator should be Shared
Also the upper number is exclusive, whereas the lower is inclusive so that should be randNumGenerator.Next(0, 10) to give numbers from 0-9
@MattWilko Agreed. Just was making a quick example but I should have specified that.
@MattWilko I edited and fixed the randNumGenerator.Next line per your comment. Good catch.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.