echo a random name from a list, vbscript

断了今生、忘了曾经 提交于 2021-02-16 20:57:51

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!