How can I generate random 8 character alphanumeric strings in C#?
相关问题
- Sorting 3 numbers without branching [closed]
- Graphics.DrawImage() - Throws out of memory except
- Generic Generics in Managed C++
- Why am I getting UnauthorizedAccessException on th
- 求获取指定qq 资料的方法
Another option could be to use Linq and aggregate random chars into a stringbuilder.
A solution without using
Random
:I know this one is not the best way. But you can try this.
I don't know how cryptographically sound this is, but it's more readable and concise than the more intricate solutions by far (imo), and it should be more "random" than
System.Random
-based solutions.I can't decide if I think this version or the next one is "prettier", but they give the exact same results:
Granted, it isn't optimized for speed, so if it's mission critical to generate millions of random strings every second, try another one!
NOTE: This solution doesn't allow for repetitions of symbols in the alphabet, and the alphabet MUST be of equal or greater size than the output string, making this approach less desirable in some circumstances, it all depends on your use-case.
Very simple solution. It uses ASCII values and just generates "random" characters in between them.
We also use custom string random but we implemented is as a string's helper so it provides some flexibility...
Usage
or