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 资料的方法
I heard LINQ is the new black, so here's my attempt using LINQ:
(Note: The use of the Random class makes this unsuitable for anything security related, such as creating passwords or tokens. Use the RNGCryptoServiceProvider class if you need a strong random number generator.)
The main goals of my code are:
The first property is achieved by taking a 64 bit value modulo the alphabet size. For small alphabets (such as the 62 characters from the question) this leads to negligible bias. The second and third property are achieved by using
RNGCryptoServiceProvider
instead ofSystem.Random
.A slightly cleaner version of DTB's solution.
Your style preferences may vary.
You just use the assembly
SRVTextToImage
. And write below code to generate random string.Mostly it is used to implement the Captcha. But in your case it also works. Hope it helps.
Now in one-liner flavour.