How to Choose Random number from given list of numbers in VB.Net

后端 未结 4 604
清酒与你
清酒与你 2021-01-29 03:35

I want to create a random number generator in VB.NET But from my own given list of numbers

Like Chose random numbers fro

4条回答
  •  失恋的感觉
    2021-01-29 03:46

    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
    

提交回复
热议问题