问题
I'm very new with programming, and I want to make a program that randomly selects a name or sentence from a list in Vb Script.
here is the list:
Jacob
James
Jason
Caleb
Ashlee
John
The program needs to choose a random name from that list.
If there is anyone who could help, I would very much appreciate it, thanks
回答1:
There is no built-in method to do what you require in VBScript. You have to implement your own as below.
Also, you might want to check these out:
- Randomize Statement
- Rnd Function
- Array Function
- UBound Function
- LBound Function
Randomize
Function RandomWithinRange(min, max)
RandomWithinRange = Int((max - min + 1) * Rnd() + min)
End Function
Function RandItemFromArray(arr)
RandItemFromArray= arr(RandomWithinRange(LBound(arr), UBound(arr)))
End Function
Dim names
names = Array("Jacob", "James", "Jason", "Caleb", "Ashlee", "John")
MsgBox RandItemFromArray(names)
回答2:
You can try like this :
Option Explicit
Dim names,index
Randomize
names = Array("Jacob","James","Jason","Caleb","Ashlee","John","Mike","Ken","Mark","Kim","Cindy","Joe")
index = Int((UBound(names)+1)*Rnd())
msgbox names(index)
回答3:
Use class Random as the index of the list.
Dim namesList As New List(Of String)
namesList.Add("John")
namesList.Add("Mike")
//etc
Dim randomNum = new Random()
Dim randomName = namesList(randumNum.Next(0, namesList.Count))
来源:https://stackoverflow.com/questions/29088793/echo-a-random-name-from-a-list-vbscript