简体   繁体   English

如何随机选择三个字符串之一?

[英]How can I randomly select one of three strings?

I have to create a password and I believe that I can do it, however I am stumped at the very beginning. 我必须创建一个密码,并且我相信可以做到,但是一开始我很困惑。 I have 3 strings of characters, I would like to randomly select one of these three strings. 我有3个字符串,我想随机选择这三个字符串之一。 Does anybody know how to do it? 有人知道怎么做吗?

  Dim sLowerCase As String = "qwertyuiopasdfghjklzxcvbnm"
        Dim sUpperCase As String = "MNBVCXZLKJHGFDSAPOIUYTREWQ"
        Dim sNumbers As String = "1234567890"

So I want to randomly select one of these three strings 所以我想随机选择这三个字符串之一

I suggest you to use ListOf string instead for this. 我建议您为此使用ListOf字符串。 you can code like the following; 您可以像下面这样编码:

    Dim listofStrings As New List(Of String) ' Declaration of list of strings
    listofStrings.Add("qwertyuiopasdfghjklzxcvbnm") 'assign values to the list
    listofStrings.Add("MNBVCXZLKJHGFDSAPOIUYTREWQ")
    listofStrings.Add("1234567890")
    Dim rnd As New Random
    Dim randomString As String = listofStrings.Item(rnd.Next(0, 3))'select random string from the list

it will generate random numbers between 0 and 2 hence it will help you to select random string from the list of strings based on index referenced by the random number 它将生成0到2之间的随机数,因此将帮助您根据随机数引用的索引从字符串列表中选择随机字符串

The below code will help you generate a random number and then pick up the string at that position in the array 下面的代码将帮助您生成一个随机数,然后在数组中的该位置拾取字符串

string[] arr= {"qwertyuiopasdfghjklzxcvbnm","MNBVCXZLKJHGFDSAPOIUYTREWQ","1234567890"};
               Random rnd = new Random();
               int cnt = rnd.Next(2);
               string r = arr[cnt];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM