gpt4 book ai didi

c# - 如何在C#数组字符串中选择5位随机数?

转载 作者:太空宇宙 更新时间:2023-11-03 17:43:04 25 4
gpt4 key购买 nike

Possible Duplicate:
How can I generate random 8 character, alphanumeric strings in C#?




我有字母名称的数组。现在我需要随机选择5个字符。如果我使用以下代码,我只能选择一个字符。如何在给定的数组字符串中随机选择5位数字?

    String[] letters = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q" };
Random rand = new Random();
String number = letters[rand.Next(1,10)];

最佳答案

string number = 
string.Concat(Enumerable.Range(0,5).Select(i =>
letters[rand.Next(0, 10)]));


当然,给定数组的内容,这在功能上等效于:

string number = rand.Next(0, 100000).ToString("D5");

关于c# - 如何在C#数组字符串中选择5位随机数? ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13414837/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com