自己写个随机数生成器有些好处,比如可以保证跨了语言都可以获得同样结果。于是查了一下资料,做了一个简单一些的实现,此处用的float,正常情况一般应该用double。
using System; public class RandomGenerator { int seed; int num1; int num2; public RandomGenerator() : this(DateTime.Now.Millisecond) { } public RandomGenerator(int argSeed) : this(argSeed,123456,789) { seed = argSeed; } public RandomGenerator(int argSeed,int argNum1,int argNum2) { seed = argSeed; num1 = argNum1; num2 = argNum2; } public int GetNumber() { return GetNumber(100); } public int GetNumber(int argLimit) { int x = num1; x++; seed *= num2; seed++; seed %= x; float y = (float)seed / (float)num1; return (int)(argLimit * y); } }