Newbie Coder
Newbie Coder

Reputation: 23

Randomising String Array

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()

Upvotes: 0

Views: 44

Answers (1)

Lews Therin
Lews Therin

Reputation: 3777

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))

Upvotes: 1

Related Questions