C# 选择随机字符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15249138/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Pick random char
提问by
i have some chars:
我有一些字符:
chars = "$%#@!*abcdefghijklmnopqrstuvwxyz1234567890?;:ABCDEFGHIJKLMNOPQRSTUVWXYZ^&".ToCharArray();
now i'm looking for a method to return a random char from these.
现在我正在寻找一种方法来从这些返回随机字符。
I found a code which maybe can be usefull:
我找到了一个可能有用的代码:
static Random random = new Random();
public static char GetLetter()
{
// This method returns a random lowercase letter
// ... Between 'a' and 'z' inclusize.
int num = random.Next(0, 26); // Zero to 25
char let = (char)('a' + num);
return let;
}
this code returns me a random char form the alphabet but only returns me lower case letters
此代码从字母表中返回一个随机字符,但只返回小写字母
回答by Jon Skeet
Well you're nearly there - you want to return a random element from a string, so you just generate a random number in the range of the length of the string:
好吧,您就快到了 - 您想从字符串中返回一个随机元素,因此您只需在字符串长度范围内生成一个随机数:
public static char GetRandomCharacter(string text, Random rng)
{
int index = rng.Next(text.Length);
return text[index];
}
I'd advise againstusing a static
variable of type Random
without any locking, by the way - Random
isn't thread-safe. See my article on random numbersfor more details (and workarounds).
顺便说一下,我建议不要使用没有任何锁定static
的类型的变量Random
-Random
不是线程安全的。有关更多详细信息(和解决方法),请参阅我关于随机数的文章。
回答by Matt
private static void Main(string[] args)
{
Console.WriteLine(GetLetters(6));
Console.ReadLine();
}
public static string GetLetters(int numberOfCharsToGenerate)
{
var random = new Random();
char[] chars = "$%#@!*abcdefghijklmnopqrstuvwxyz1234567890?;:ABCDEFGHIJKLMNOPQRSTUVWXYZ^&".ToCharArray();
var sb = new StringBuilder();
for (int i = 0; i < numberOfCharsToGenerate; i++)
{
int num = random.Next(0, chars.Length);
sb.Append(chars[num]);
}
return sb.ToString();
}
回答by Soner G?nül
回答by Yaugen Vlasau
Instead of 26 please use size of your CHARS buffer.
请使用 CHARS 缓冲区的大小而不是 26。
int num = random.Next(0, chars.Length)
Then instead of
然后代替
let = (char)('a' + num)
use
用
let = chars[num]
回答by Saman Gholami
I wish This code helps you :
我希望此代码可以帮助您:
string s = "$%#@!*abcdefghijklmnopqrstuvwxyz1234567890?;:ABCDEFGHIJKLMNOPQRSTUVWXYZ^&";
Random random = new Random();
int num = random.Next(0, s.Length -1);
MessageBox.Show(s[num].ToString());
回答by Volodymyr Machula
I had approximate issue and I did it by this way:
我有近似的问题,我是通过这种方式做到的:
public static String GetRandomString()
{
var allowedChars = "abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNOPQRSTUVWXYZ0123456789";
var length = 15;
var chars = new char[length];
var rd = new Random();
for (var i = 0; i < length; i++)
{
chars[i] = allowedChars[rd.Next(0, allowedChars.Length)];
}
return new String(chars);
}
回答by jordanhill123
This might work for you:
这可能对你有用:
public static char GetLetter()
{
string chars = "$%#@!*abcdefghijklmnopqrstuvwxyz1234567890?;:ABCDEFGHIJKLMNOPQRSTUVWXYZ^&";
Random rand = new Random();
int num = rand.Next(0, chars.Length -1);
return chars[num];
}
回答by Shirish Joshi
Getting Character from ASCII number:
从 ASCII 数字获取字符:
private string GenerateRandomString()
{
Random rnd = new Random();
string txtRand = string.Empty;
for (int i = 0; i <8; i++) txtRand += ((char)rnd.Next(97, 122)).ToString();
return txtRand;
}
回答by Shirish Joshi
You can try this :
你可以试试这个:
public static string GetPassword()
{
string Characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
Random rnd = new Random();
int index = rnd.Next(0,51);
string char1 = Characters[index].ToString();
return char1;
}
Now you can play with this code block as per your wish. Cheers!
现在您可以根据自己的意愿使用此代码块。干杯!
回答by Decon21
I'm not sure how efficient it is as I'm very new to coding, however, why not just utilize the random number your already creating? Wouldn't this "randomize" an uppercase char as well?
我不确定它的效率如何,因为我对编码很陌生,但是,为什么不直接使用您已经创建的随机数呢?这不会“随机化”一个大写字符吗?
int num = random.Next(0,26);
char let = (num > 13) ? Char.ToUpper((char)('a' + num)) : (char)('a' + num);
Also, if you're looking to take a single letter from your char[], would it be easier to just use a string?
另外,如果您想从 char[] 中获取单个字母,那么仅使用字符串会更容易吗?
string charRepo = "$%#@!*abcdefghijklmnopqrstuvwxyz1234567890?;:ABCDEFGHIJKLMNOPQRSTUVWXYZ^&";
Random rando = new Random();
int ranNum = rando.Next(0, charRepo.Length);
char ranChar = charRepo[ranNum];