如何从 VB.Net 中给定的数字列表中选择随机数
问题描述:
我想在 VB.NET 中创建一个随机数生成器但是从我自己给定的数字列表中
I want to create a random number generator in VB.NET But from my own given list of numbers
喜欢从 [1,2,3,4,5,6] e.t.c 中选择随机数
答
已经内置到 .NET 基础 'Random' 中,然后将其扩展到您现有的选择中.这与从 Random 生成数字不同,因为您首先指定您自己的列表,然后仅在新 Rand 的帮助下进行定位并将您的长度用作它的上限.
Already built into .NET base of 'Random' and then extending that into your existing choices. This is NOT the same as generating the number from a Random as you are specifying YOUR OWN list first and then merely getting positioning with the help of a new Rand and using your length as a ceiling for it.
Sub Main()
'Say you have four items in your list
Dim ls = New List(Of Integer)({1, 4, 8, 20})
'I can find the 'position' of where the count of my array could be
Dim rand = New Random().Next(0, ls.Count)
'This will give a different 'position' every time.
Console.WriteLine(ls(rand))
Console.ReadLine()
End Sub