예제 #1
0
        /// <summary>
        /// Genetic mutation for auxiliary argument data.
        /// </summary>
        public void MutateAuxArgs(double[] auxArgs, XorShiftRandom rng, ZigguratGaussianSampler gaussianSampler, double connectionWeightRange)
        {
            // Mutate center.
            // Add gaussian ditribution sample and clamp result to +-connectionWeightRange.
            double tmp = auxArgs[0] + gaussianSampler.NextSample(0, _auxArgsMutationSigmaCenter);

            if (tmp < -connectionWeightRange)
            {
                auxArgs[0] = -connectionWeightRange;
            }
            else if (tmp > connectionWeightRange)
            {
                auxArgs[0] = connectionWeightRange;
            }
            else
            {
                auxArgs[0] = tmp;
            }

            // Mutate radius.
            // Add gaussian ditribution sample and clamp result to [0,1]
            tmp = auxArgs[1] + gaussianSampler.NextSample(0, _auxArgsMutationSigmaRadius);
            if (tmp < 0.0)
            {
                auxArgs[1] = 0.0;
            }
            else if (tmp > 1.0)
            {
                auxArgs[1] = 1.0;
            }
            else
            {
                auxArgs[1] = tmp;
            }
        }
예제 #2
0
        public void NextBool()
        {
            int            sampleCount = 10000000;
            XorShiftRandom rng         = new XorShiftRandom();

            int    trueCount = 0, falseCount = 0;
            double maxExpectedCountErr = sampleCount / 25.0;

            for (int i = 0; i < sampleCount; i++)
            {
                if (rng.NextBool())
                {
                    trueCount++;
                }
                else
                {
                    falseCount++;
                }
            }

            double countErr = Math.Abs(trueCount - falseCount);

            if (countErr > maxExpectedCountErr)
            {
                Assert.Fail();
            }
        }
예제 #3
0
 /// <summary>
 /// For activation functions that accept auxiliary arguments; generates random initial values for aux arguments for newly
 /// added nodes (from an 'add neuron' mutation).
 /// </summary>
 public double[] GetRandomAuxArgs(XorShiftRandom rng, double connectionWeightRange)
 {
     double[] auxArgs = new double[2];
     auxArgs[0] = (rng.NextDouble() - 0.5) * 2.0;
     auxArgs[1] = rng.NextDouble();
     return(auxArgs);
 }
예제 #4
0
        public void TestRangeRandomOrder()
        {
            var rng = new XorShiftRandom(0);

            RunTest(0, 100, rng);
            RunTest(20, 100, rng);
        }
        protected BaseBenchmark()
        {
            var seed = Environment.TickCount;

            Random   = new Random(seed);
            XorShift = new XorShiftRandom(seed);
            XorShiRo = new XorShiRoRandom(seed);
        }
예제 #6
0
        internal LocalSpriteData(ReadOnlySpan <Vector2I> smallSizes, string path)
        {
            Path = path;
            Name = path;
            Rand = new(path.GetLongHashCode());

            Data = Preload(smallSizes);
        }
예제 #7
0
        public void NextBytes()
        {
            int            sampleCount = 10000000;
            XorShiftRandom rng         = new XorShiftRandom();

            byte[] sampleArr = new byte[sampleCount];
            rng.NextBytes(sampleArr);
            NextByteInner(sampleArr);
        }
예제 #8
0
 /// <summary>
 /// Constructs with the provided world parameter arguments.
 /// </summary>
 public PreyCaptureWorld(int gridSize, int preyInitMoves, double preySpeed, double sensorRange, int maxTimesteps)
 {
     _gridSize      = gridSize;
     _preyInitMoves = preyInitMoves;
     _preySpeed     = preySpeed;
     _sensorRange   = sensorRange;
     _maxTimesteps  = maxTimesteps;
     _rng           = new XorShiftRandom();
 }
예제 #9
0
        public void TestProbabilisticRound()
        {
            var rng = new XorShiftRandom(0);

            for (int i = 0; i < 1000000; i++)
            {
                double valReal  = 100 * rng.NextDouble();
                double valRound = NumericsUtils.ProbabilisticRound(valReal, rng);
                Assert.IsTrue(valRound == Math.Floor(valReal) || valRound == Math.Ceiling(valReal));
            }
        }
예제 #10
0
        public void NextByte()
        {
            int            sampleCount = 10000000;
            XorShiftRandom rng         = new XorShiftRandom();

            byte[] sampleArr = new byte[sampleCount];
            for (int i = 0; i < sampleCount; i++)
            {
                sampleArr[i] = rng.NextByte();
            }
            NextByteInner(sampleArr);
        }
예제 #11
0
        public MeshData(int maxInstances, int size)
        {
            MaxInstances = maxInstances;
            Size         = size;

            Random = new XorShiftRandom();

            Vertices  = new Vector3[VertsPerQuad * maxInstances];
            Triangles = new int[TrisPerQuad * maxInstances];
            Uvs       = new Vector2[VertsPerQuad * maxInstances];
            Normals   = new Vector3[VertsPerQuad * maxInstances];
            Bounds    = new Bounds();
        }
예제 #12
0
        public Color[] NextFrame()
        {
            Parallel.For(0, renderConfig.Height, y =>
            {
                Parallel.For(0, renderConfig.Width, x =>
                {
                    // chapter 1 outputs the image to the image file, we're flipping the
                    // row pointer so that we start from the other end in order to get same visual result.
                    var index = (renderConfig.Height - 1 - y) * renderConfig.Width + x;

                    if (renderConfig.Antialiasing)
                    {
                        Vector3 color = new Vector3();

                        for (int i = 0; i < renderConfig.AASamples; i++)
                        {
                            float u = (x + XorShiftRandom.NextFloat()) / renderConfig.Width;
                            float v = (y + XorShiftRandom.NextFloat()) / renderConfig.Height;

                            color += PixelColor(camera.GetRay(u, v), World, 0);
                        }

                        color /= renderConfig.AASamples;
                        color  = new Vector3(MathUtil.FastSqrt(color.X), MathUtil.FastSqrt(color.Y), MathUtil.FastSqrt(color.Z));

                        color *= 255.99f;
                        frameBuffer[index].R = (byte)color.X;
                        frameBuffer[index].G = (byte)color.Y;
                        frameBuffer[index].B = (byte)color.Z;
                        frameBuffer[index].A = 255;
                    }
                    else
                    {
                        float u = (float)x / renderConfig.Width;
                        float v = (float)y / renderConfig.Height;

                        var color = PixelColor(camera.GetRay(u, v), World, 0);

                        color = new Vector3(MathUtil.FastSqrt(color.X), MathUtil.FastSqrt(color.Y), MathUtil.FastSqrt(color.Z));

                        color *= 255.99f;
                        frameBuffer[index].R = (byte)color.X;
                        frameBuffer[index].G = (byte)color.Y;
                        frameBuffer[index].B = (byte)color.Z;
                        frameBuffer[index].A = 255;
                    }
                });
            });

            return(frameBuffer);
        }
예제 #13
0
 /// <summary>
 /// Randomly shuffles items within a list.
 /// </summary>
 /// <param name="list">The list to shuffle.</param>
 /// <param name="rng">Random number generator.</param>
 public static void Shuffle <T>(IList <T> list, XorShiftRandom rng)
 {
     // This approach was suggested by Jon Skeet in a dotNet newsgroup post and
     // is also the technique used by the OpenJDK. The use of rnd.Next(i+1) introduces
     // the possibility of swapping an item with itself, I suspect the reasoning behind this
     // has to do with ensuring the probability of each possible permutation is approximately equal.
     for (int i = list.Count - 1; i > 0; i--)
     {
         int swapIndex = rng.Next(i + 1);
         T   tmp       = list[swapIndex];
         list[swapIndex] = list[i];
         list[i]         = tmp;
     }
 }
예제 #14
0
        public void NextFloat()
        {
            int            sampleCount = 10000000;
            XorShiftRandom rng         = new XorShiftRandom();

            double[] sampleArr = new double[sampleCount];

            for (int i = 0; i < sampleCount; i++)
            {
                sampleArr[i] = rng.NextFloat();
            }

            UniformDistributionTest(sampleArr, 0.0, 1.0);
        }
예제 #15
0
        public void NextLowerUpper()
        {
            int            sampleCount = 10000000;
            XorShiftRandom rng         = new XorShiftRandom();

            double[] sampleArr = new double[sampleCount];

            for (int i = 0; i < sampleCount; i++)
            {
                sampleArr[i] = rng.Next(1000000, 1234567);
            }

            UniformDistributionTest(sampleArr, 1000000, 1234567);
        }
예제 #16
0
        public void NextBytes_LengthNotMultipleOfFour()
        {
            int            sampleCount = 10000003;
            XorShiftRandom rng         = new XorShiftRandom(0);

            byte[] sampleArr = new byte[sampleCount];
            rng.NextBytes(sampleArr);
            NextByteInner(sampleArr);

            // Note. We want to check that the last three bytes are being assigned random bytes, but the RNG
            // can generate zeroes, so this test is reliant on the RNG seed being fixed to ensure we have non-zero
            // values in those elements each time the test is run.
            Assert.IsTrue(sampleArr[sampleCount - 1] != 0);
            Assert.IsTrue(sampleArr[sampleCount - 2] != 0);
            Assert.IsTrue(sampleArr[sampleCount - 3] != 0);
        }
예제 #17
0
        /// <summary>
        /// Randomly shuffles a sub-span of items within a list.
        /// </summary>
        /// <param name="list">The list to shuffle.</param>
        /// <param name="rng">Random number generator.</param>
        /// <param name="startIdx">The index of the first item in the segment.</param>
        /// <param name="endIdx">The index of the last item in the segment, i.e. endIdx is inclusive; the item at endIdx will participate in the shuffle.</param>
        public static void Shuffle <T>(IList <T> list, XorShiftRandom rng, int startIdx, int endIdx)
        {
            // Determine how many items in the list will be being shuffled
            int itemCount = (endIdx - startIdx);

            // This approach was suggested by Jon Skeet in a dotNet newsgroup post and
            // is also the technique used by the OpenJDK. The use of rnd.Next(i+1) introduces
            // the possibility of swapping an item with itself, I suspect the reasoning behind this
            // has to do with ensuring the probability of each possible permutation is approximately equal.
            for (int i = endIdx; i > startIdx; i--)
            {
                int swapIndex = startIdx + rng.Next((i - startIdx) + 1);
                T   tmp       = list[swapIndex];
                list[swapIndex] = list[i];
                list[i]         = tmp;
            }
        }
예제 #18
0
        public void NextDoubleNonZero()
        {
            int            sampleCount = 10000000;
            XorShiftRandom rng         = new XorShiftRandom();

            double[] sampleArr = new double[sampleCount];

            for (int i = 0; i < sampleCount; i++)
            {
                sampleArr[i] = rng.NextDoubleNonZero();
                if (0.0 == sampleArr[i])
                {
                    Assert.Fail();
                }
            }

            UniformDistributionTest(sampleArr, 0.0, 1.0);
        }
예제 #19
0
        public void NextLowerUpper_LongRange_Distribution()
        {
            int            sampleCount = 10000000;
            XorShiftRandom rng         = new XorShiftRandom();

            int maxValHalf = int.MaxValue / 2;
            int lowerBound = -(maxValHalf + 10000);
            int upperBound = (maxValHalf + 10000);

            // N.B. double precision can represent every Int32 value exactly.
            double[] sampleArr = new double[sampleCount];
            for (int i = 0; i < sampleCount; i++)
            {
                sampleArr[i] = rng.Next(lowerBound, upperBound);
            }

            UniformDistributionTest(sampleArr, lowerBound, upperBound);
        }
예제 #20
0
 public MemoryStreamFuzzer(MemoryStream strmA, MemoryBlockStream strmB, int seed)
 {
     _strmA          = strmA;
     _strmB          = strmB;
     _rng            = new XorShiftRandom(seed);
     _opDistribution = new DiscreteDistribution(_rng,
                                                new double[]
     {
         0.688,      // Write
         0.05,       // Write byte
         0.05,       // Change read/write head position.
         0.05,       // SetLength
         0.05,       // Seek
         0.002,      // Trim
         0.01,       // Read byte
         0.1,        // Read
     });
 }
예제 #21
0
        public void SampleUniformWithoutReplacement_SampleAllChoices()
        {
            const int      size = 5;
            XorShiftRandom rng  = new XorShiftRandom();

            // Sample all of the elements.
            int[] sampleArr = new int[size];
            DiscreteDistributionUtils.SampleUniformWithoutReplacement(size, sampleArr, rng);

            // Sort the samples.
            Array.Sort(sampleArr);

            // Confirm that all of the choices were selected.
            for (int i = 0; i < size; i++)
            {
                Assert.AreEqual(i, sampleArr[i]);
            }
        }
예제 #22
0
        public void TestWriteZeroBytes()
        {
            byte[]            buf = new byte[0];
            MemoryBlockStream ms  = new MemoryBlockStream();

            ms.Write(buf, 0, 0);
            Assert.AreEqual(ms.Length, 0);

            XorShiftRandom rng = new XorShiftRandom(1234567);

            byte[] buf2 = new byte[100];
            rng.NextBytes(buf2);
            ms.Write(buf2, 0, buf2.Length);

            if (!Utils.AreEqual(ms.ToArray(), buf2))
            {
                Assert.Fail();
            }

            ms.Write(buf, 0, 0);
            Assert.AreEqual(ms.Length, buf2.Length);
        }
예제 #23
0
        /// <summary>
        /// Sort the items in the provided list. In addition we ensure that items that have are defined as equal by the IComparer
        /// are arranged randomly.
        /// </summary>
        /// <typeparam name="T">The type of elements in the list.</typeparam>
        /// <param name="list">The list of items to sort.</param>
        /// <param name="comparer">The IComparer<T> implementation to use when comparing elements.</param>
        /// <param name="rng">Random number generator.</param>
        public static void SortUnstable <T>(List <T> list, IComparer <T> comparer, XorShiftRandom rng)
        {
            // ENHANCEMENT: The naive approach is to shuffle the list items and then call Sort(); regardless of whether the
            // sort is stable or not, the equal items would be arranged randomly (with an even distribution across all possible
            // locations).
            // However, typically lists are already partially sorted and this improves the performance of the sort. To try and
            // keep some of that benefit we could call sort first, and then call shuffle on sub-sgments of items identified as equal.
            if (list.Count < 10)
            {
                Shuffle(list, rng);
                list.Sort(comparer);
                return;
            }

            // Sort the list.
            list.Sort(comparer);

            // Scan for segments of items that are equal.
            int startIdx = 0;
            int endIdx;
            int count = list.Count;

            while (TryFindSegment(list, comparer, ref startIdx, out endIdx))
            {
                // Shuffle the segment of equal items.
                Shuffle(list, rng, startIdx, endIdx);

                // Test for the end of the list.
                // N.B. If endIdx points to one of the last two items then there can be no more segments (segments are made of at least two items).
                if (endIdx > count - 3)
                {
                    break;
                }

                // Set the startIdx of the next candidate segment.
                startIdx = endIdx + 1;
            }
        }
예제 #24
0
        public void NextLowerUpper_LongRange_Bounds()
        {
            int            sampleCount = 10000000;
            XorShiftRandom rng         = new XorShiftRandom();

            System.Random sysRng = new System.Random();

            int maxValHalf = int.MaxValue / 2;

            double[] sampleArr = new double[sampleCount];

            for (int i = 0; i < sampleCount; i++)
            {
                int lowerBound = -(maxValHalf + (sysRng.Next() / 2));
                int upperBound = (maxValHalf + (sysRng.Next() / 2));
                int sample     = rng.Next(lowerBound, upperBound);

                if (sample < lowerBound || sample >= upperBound)
                {
                    Assert.Fail();
                }
            }
        }
예제 #25
0
        private List <Hitable> RandomScene()
        {
            var result = new List <Hitable>();

            result.Add(new Sphere(new Vector3(0f, -1000f, 0f), 1000f, new Lambertian(new Vector3(0.5f, 0.5f, 0.5f))));
            result.Add(new Sphere(new Vector3(0f, 1f, 0f), 1f, new Dielectric(1.5f)));
            result.Add(new Sphere(new Vector3(-4f, 1f, 0f), 1f, new Lambertian(new Vector3(0.4f, 0.2f, 0.1f))));
            result.Add(new Sphere(new Vector3(4f, 1f, 0f), 1f, new Metal(new Vector3(0.7f, 0.6f, 0.5f), 0.0f)));

            for (int a = -11; a < 11; a++)
            {
                for (int b = -11; b < 11; b++)
                {
                    float   material = XorShiftRandom.NextFloat();
                    Vector3 center   = new Vector3(a + 0.9f * XorShiftRandom.NextFloat(), 0.2f, b + 0.9f * XorShiftRandom.NextFloat());

                    if ((center - new Vector3(4f, 0f, 0.2f)).Length() > 0.9f)
                    {
                        if (material < 0.8f)
                        {
                            result.Add(new Sphere(center, 0.2f, new Lambertian(new Vector3(XorShiftRandom.NextFloat() * XorShiftRandom.NextFloat(), XorShiftRandom.NextFloat() * XorShiftRandom.NextFloat(), XorShiftRandom.NextFloat() * XorShiftRandom.NextFloat()))));
                        }
                        else if (material < 0.95f)
                        {
                            result.Add(new Sphere(center, 0.2f, new Metal(new Vector3(0.5f * (1 + XorShiftRandom.NextFloat()), 0.5f * (1 + XorShiftRandom.NextFloat()), 0.5f * (1 + XorShiftRandom.NextFloat())), 0.5f * XorShiftRandom.NextFloat())));
                        }
                        else
                        {
                            result.Add(new Sphere(center, 0.2f, new Dielectric(1.5f)));
                        }
                    }
                }
            }

            return(result);
        }
예제 #26
0
 /// <summary>
 /// Construct evaluator with the provided task arguments/variables.
 /// </summary>
 public WalkerBox2dEvaluator(int maxTimesteps)
 {
     _rng          = new XorShiftRandom();
     _maxTimesteps = maxTimesteps;
 }
예제 #27
0
 /// <summary>
 /// For activation functions that accept auxiliary arguments; generates random initial values for aux arguments for newly
 /// added nodes (from an 'add neuron' mutation).
 /// </summary>
 public double[] GetRandomAuxArgs(XorShiftRandom rng, double connectionWeightRange)
 {
     throw new SharpNeatException("GetRandomAuxArgs() called on activation function that does not use auxiliary arguments.");
 }
예제 #28
0
 /// <summary>
 /// Genetic mutation for auxiliary argument data.
 /// </summary>
 public void MutateAuxArgs(double[] auxArgs, XorShiftRandom rng, ZigguratGaussianSampler gaussianSampler, double connectionWeightRange)
 {
     throw new SharpNeatException("MutateAuxArgs() called on activation function that does not use auxiliary arguments.");
 }
예제 #29
0
 /// <summary>
 /// Default constructor.
 /// </summary>
 public TestCaseField()
 {
     _rng = new XorShiftRandom();
 }
예제 #30
0
 /// <summary>
 /// Constructor accepting a trackLength parameter (length of the track that the walker is walking along).
 /// </summary>
 /// <param name="trackLength"></param>
 /// <param name="rng">Random number generator.</param>
 public WalkerWorld(XorShiftRandom rng, float trackLength)
 {
     _rng             = rng;
     _trackLength     = trackLength;
     _trackLengthHalf = trackLength * 0.5f;
 }