How can I randomly select one of three strings?

后端 未结 2 1120
你的背包
你的背包 2021-01-29 09:10

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 th

相关标签:
2条回答
  • 2021-01-29 09:38

    I suggest you to use ListOf string instead for this. 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 讨论(0)
  • 2021-01-29 09:38

    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];
    
    0 讨论(0)
提交回复
热议问题