Random rnd = new Random();int month = rnd.Next(1, 13); // creates a number between 1 and 12int dice = rnd.Next(1, 7); // creates a number between 1 and 6int card = rnd.Next(52); // creates a number between 0 and 51
//Function to get random numberprivate static readonly Random getrandom = new Random();
public static int GetRandomNumber(int min, int max){lock(getrandom) // synchronize{return getrandom.Next(min, max);}}
// Check to see if this is a compatible CPUbool isAvailable = RdRandom.GeneratorAvailable();
// Generate 10 random charactersstring key = RdRandom.GenerateKey(10);
// Generate 64 random characters, useful for API keysstring apiKey = RdRandom.GenerateAPIKey();
// Generate an array of 10 random bytesbyte[] b = RdRandom.GenerateBytes(10);
// Generate a random unsigned intuint i = RdRandom.GenerateUnsignedInt();
string ret = Randomizer.GenerateKey(<length>, "<key>");uint ret = Randomizer.GenerateUInt("<key>");byte[] ret = Randomizer.GenerateBytes(<length>, "<key>");
internal static class RandomNumber{private static Random r = new Random();private static object l = new object();private static Random globalRandom = new Random();[ThreadStatic]private static Random localRandom;public static int GenerateNewRandom(int min, int max){return new Random().Next(min, max);}public static int GenerateLockedRandom(int min, int max){int result;lock (RandomNumber.l){result = RandomNumber.r.Next(min, max);}return result;}public static int GenerateRandom(int min, int max){Random random = RandomNumber.localRandom;if (random == null){int seed;lock (RandomNumber.globalRandom){seed = RandomNumber.globalRandom.Next();}random = (RandomNumber.localRandom = new Random(seed));}return random.Next(min, max);}}
public int GenerateRandom(int min, int max){var seed = Convert.ToInt32(Regex.Match(Guid.NewGuid().ToString(), @"\d+").Value);return new Random(seed).Next(min, max);}
更新:如果您实例化Random类一次,则不需要播种。所以最好创建一个静态类并调用一个方法。
public static class IntUtil{private static Random random;
private static void Init(){if (random == null) random = new Random();}
public static int Random(int min, int max){Init();return random.Next(min, max);}}
然后你可以像这样使用静态类…
for(var i = 0; i < 1000; i++){int randomNumber = IntUtil.Random(1,100);Console.WriteLine(randomNumber);}
using (RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider()){byte[] randomNumber = new byte[4];//4 for int32rng.GetBytes(randomNumber);int value = BitConverter.ToInt32(randomNumber, 0);}
public class A{public A(){var rnd=new Random();ID=rnd.Next();}public int ID { get; private set; }}public class B{public B(){var rnd=new Random();ID=rnd.Next();}public int ID { get; private set; }}
你认为你会得到两个不同的ID吗?NOPE
class Program{static void Main(string[] args){A a=new A();B b=new B();
int ida=a.ID, idb=b.ID;// ida = 1452879101// idb = 1452879101}}
解决方案是总是使用单个静态随机生成器。像这样:
public static class Utils{public static readonly Random random=new Random();}
public class A{public A(){ID=Utils.random.Next();}public int ID { get; private set; }}public class B{public B(){ID=Utils.random.Next();}public int ID { get; private set; }}
using (RNGCryptoServiceProvider rg = new RNGCryptoServiceProvider()){byte[] rno = new byte[5];rg.GetBytes(rno);int randomvalue = BitConverter.ToInt32(rno, 0);}
public class RandomGenerator{public int RandomNumber(int min, int max){var random = new Random();return random.Next(min, max);}
public string RandomString(int size, bool lowerCase){var builder = new StringBuilder();var random = new Random();char ch;
for (int i = 0; i < size; i++){ch = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));builder.Append(ch);}
if (lowerCase)return builder.ToString().ToLower();return builder.ToString();}}
using System;using System.Security.Cryptography;
public class Program{public static void Main(){var random = new Random(GetSeed());Console.WriteLine(random.Next());}
public static int GetSeed(){using (var rng = new RNGCryptoServiceProvider()){var intBytes = new byte[4];rng.GetBytes(intBytes);return BitConverter.ToInt32(intBytes, 0);}}}
// Somewhat better code...Random rng = new Random();for (int i = 0; i < 100; i++){Console.WriteLine(GenerateDigit(rng));}...static int GenerateDigit(Random rng){// Assume there'd be more logic here reallyreturn rng.Next(10);}
class SecureRandom : Random{public static byte[] GetBytes(ulong length){RNGCryptoServiceProvider RNG = new RNGCryptoServiceProvider();byte[] bytes = new byte[length];RNG.GetBytes(bytes);RNG.Dispose();return bytes;}public SecureRandom() : base(BitConverter.ToInt32(GetBytes(4), 0)){
}public int GetRandomInt(int min, int max){int treashold = max - min;if(treashold != Math.Abs(treashold)){throw new ArithmeticException("The minimum value can't exceed the maximum value!");}if (treashold == 0){throw new ArithmeticException("The minimum value can't be the same as the maximum value!");}return min + (Next() % treashold);}public static int GetRandomIntStatic(int min, int max){int treashold = max - min;if (treashold != Math.Abs(treashold)){throw new ArithmeticException("The minimum value can't exceed the maximum value!");}if(treashold == 0){throw new ArithmeticException("The minimum value can't be the same as the maximum value!");}return min + (BitConverter.ToInt32(GetBytes(4), 0) % treashold);}}
// Gives a random number for the integer range.// You can simply update the parameters as your needs.RandomNumberGenerator.GetInt32(int.MinValue, int.MaxValue);