示例#1
0
        public void TestIRandomExtensionMethods()
        {
            IRandom rand = new TinyMT();

            rand.Init(1);                            // 2545341989 will be the first number
            Assert.IsTrue(rand.Generate(0, 5) == 4); // 2545341989 % 5 = 4

            rand.Init(1);
            Assert.IsTrue(rand.Generate(1, 6) == 5); // 2545341989 % 5 + 1 = 5

            rand.Init(1);
            Assert.IsTrue(rand.PickElement(new uint[] { 0, 1, 2, 3, 4 }) == 4); // 2545341989 % 5 = 4, so 4th element

            rand.Init(1);
            Assert.IsTrue(rand.DieRoll(10) == 10); // last digit of 2545341989 plus one

            RandomRange range = new RandomRange(0, 10, 3);

            rand.Init(1);
            // first random number: 2545341989 % 10 = 9
            // second random number: 981918433 % 10 = 3
            // third random number: 3715302833 % 10 = 3
            // total: 15, over three passes: result = 5
            Assert.IsTrue(rand.Generate(range) == 5);
        }
示例#2
0
        /// <summary>
        /// Generates the next random value in the half-open range [range.min, range.max)
        /// with a distribution specified by the range.passes value.
        /// </summary>
        /// <param name="rng"></param>
        /// <param name="range"></param>
        /// <returns></returns>
        public static int Generate(this IRandom rng, RandomRange range)
        {
            long total = 0;

            for (int i = 0; i < range.passes; i++)
            {
                total += rng.Generate(range.from, range.to);
            }
            return((int)(total / range.passes));
        }