Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我的字符串数组有一个单词列表,例如 man、ran 等。有人可以帮我从字符串数组中随机选取一个单词并将其存储在变量 myword 中。
Random r = new Random(); string[] words = {"man", "rat", "cow", "chicken"}; Console.WriteLine(words[r.Next(0, words.Length)]);
或者
string word = words[r.Next(0, words.Length)];
不过使用时要小心Random。这是一个伪随机数生成器,因此如果使用不当,可能无法获得预期的结果。
Random