如何从 VB.Net 中给定的数字列表中选择随机数 [英] How to Choose Random number from given list of numbers in VB.Net

查看:22
本文介绍了如何从 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

这篇关于如何从 VB.Net 中给定的数字列表中选择随机数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆