Last Updated: February 25, 2016
·
4.683K
· iondrimba

Generating Random Numbers and Strings in C#

//RANDOM STRING
string random = System.IO.Path.GetRandomFileName().Replace(".", string.Empty);

//RANDOM PASSWORD/STRING
string randomPass = System.Web.Security.Membership.GeneratePassword(10, 0);

//RANDOM INT
System.Random randomInt = new System.Random((int)System.DateTime.Now.Ticks);
int start = 1;
int size = 1000;
int result= randomInt.Next(start, size);

source: http://blog.codeeffects.com/Article/Generate-Random-Numbers-And-Strings-C-Sharp

2 Responses
Add your response

All of the above are pseudo-random values, which is likely fine for most scenarios.

System.Security.Cryptography.RandomNumberGenerator is the base class for cryptographically secure random values that are not deterministic/predictable.

See the example @ http://msdn.microsoft.com/en-us/library/system.security.cryptography.rngcryptoserviceprovider.aspx

over 1 year ago ·

Thanks for pointing that out =]

over 1 year ago ·