Esempio n. 1
0
 // Use this for initialization
 void Start()
 {
     MathNet.Numerics.Random.CryptoRandomSource rn = new MathNet.Numerics.Random.CryptoRandomSource();
     this.gameObject.FixedUpdateAsObservable().Buffer(TimeSpan.FromMilliseconds(100)).Subscribe(x =>
     {
         if (rn.NextDouble() > 0.9d)
         {
             //set position
             GameObject cube = Instantiate(obstaclePrefab);
             ObstacleObjects.Add(cube);
             cube.transform.position = new Vector3(this.transform.position.x + SpawnStartOffset.x, this.transform.position.y + SpawnStartOffset.y, this.transform.position.z + SpawnStartOffset.z);
             //set color
             cube.GetComponent <Renderer>().material.color = Color.cyan;
             // destroy if out of bounds
             cube.FixedUpdateAsObservable().Buffer(TimeSpan.FromMilliseconds(100)).Subscribe(t =>
             {
                 float offsetX = Math.Abs(cube.transform.position.x - this.transform.position.x);
                 float offsetY = Math.Abs(cube.transform.position.y - this.transform.position.y);
                 float offsetZ = Math.Abs(cube.transform.position.z - this.transform.position.z);
                 if (offsetX > SpawnMaxOffset.x || offsetY > SpawnMaxOffset.y || offsetZ > SpawnMaxOffset.z)
                 {
                     GameObject.Destroy(cube);
                     ObstacleObjects.Remove(cube);
                 }
             }).AddTo(this);
         }
     }).AddTo(this);
     //clean up after exit
     Observable.OnceApplicationQuit().Subscribe((exit) => {
         foreach (GameObject obstacleObject in ObstacleObjects)
         {
             if (obstacleObject != null)
             {
                 GameObject.DestroyImmediate(obstacleObject);
             }
         }
     });
 }
        /// <summary>
        /// Run example
        /// </summary>
        /// <seealso cref="http://en.wikipedia.org/wiki/Random_number_generation">Random number generation</seealso>
        /// <seealso cref="http://en.wikipedia.org/wiki/Linear_congruential_generator">Linear congruential generator</seealso>
        /// <seealso cref="http://en.wikipedia.org/wiki/Mersenne_twister">Mersenne twister</seealso>
        /// <seealso cref="http://en.wikipedia.org/wiki/Lagged_Fibonacci_generator">Lagged Fibonacci generator</seealso>
        /// <seealso cref="http://en.wikipedia.org/wiki/Xorshift">Xorshift</seealso>
        public void Run()
        {
            // All RNG classes in MathNet have next counstructors:
            // - RNG(int seed, bool threadSafe): initializes a new instance with specific seed value and thread safe property
            // - RNG(int seed): iуууnitializes a new instance with specific seed value. Thread safe property is set to Control.ThreadSafeRandomNumberGenerators
            // - RNG(bool threadSafe) : initializes a new instance with the seed value set to DateTime.Now.Ticks and specific thread safe property
            // - RNG(bool threadSafe) : initializes a new instance with the seed value set to DateTime.Now.Ticks and thread safe property set to Control.ThreadSafeRandomNumberGenerators

            // All RNG classes in MathNet have next methods to produce random values:
            // - double[] NextDouble(int n): returns an "n"-size array of uniformly distributed random doubles in the interval [0.0,1.0];
            // - int Next(): returns a nonnegative random number;
            // - int Next(int maxValue): returns a random number less then a specified maximum;
            // - int Next(int minValue, int maxValue): returns a random number within a specified range;
            // - void NextBytes(byte[] buffer): fills the elements of a specified array of bytes with random numbers;

            // All RNG classes in MathNet have next extension methods to produce random values:
            // - long NextInt64(): returns a nonnegative random number less than "Int64.MaxValue";
            // - int NextFullRangeInt32(): returns a random number of the full Int32 range;
            // - long NextFullRangeInt64(): returns a random number of the full Int64 range;
            // - decimal NextDecimal(): returns a nonnegative decimal floating point random number less than 1.0;

            // 1. Multiplicative congruential generator using a modulus of 2^31-1 and a multiplier of 1132489760
            var mcg31M1 = new Mcg31m1(1);
            Console.WriteLine(@"1. Generate 10 random double values using Multiplicative congruential generator with a modulus of 2^31-1 and a multiplier of 1132489760");
            var randomValues = mcg31M1.NextDoubles(10);
            for (var i = 0; i < randomValues.Length; i++)
            {
                Console.Write(randomValues[i].ToString("N") + @" ");
            }

            Console.WriteLine();
            Console.WriteLine();

            // 2. Multiplicative congruential generator using a modulus of 2^59 and a multiplier of 13^13
            var mcg59 = new Mcg59(1);
            Console.WriteLine(@"2. Generate 10 random integer values using Multiplicative congruential generator with a modulus of 2^59 and a multiplier of 13^13");
            for (var i = 0; i < 10; i++)
            {
                Console.Write(mcg59.Next() + @" ");
            }

            Console.WriteLine();
            Console.WriteLine();

            // 3. Random number generator using Mersenne Twister 19937 algorithm
            var mersenneTwister = new MersenneTwister(1);
            Console.WriteLine(@"3. Generate 10 random integer values less then 100 using Mersenne Twister 19937 algorithm");
            for (var i = 0; i < 10; i++)
            {
                Console.Write(mersenneTwister.Next(100) + @" ");
            }

            Console.WriteLine();
            Console.WriteLine();

            // 4. Multiple recursive generator with 2 components of order 3
            var mrg32K3A = new Mrg32k3a(1);
            Console.WriteLine(@"4. Generate 10 random integer values in range [50;100] using multiple recursive generator with 2 components of order 3");
            for (var i = 0; i < 10; i++)
            {
                Console.Write(mrg32K3A.Next(50, 100) + @" ");
            }

            Console.WriteLine();
            Console.WriteLine();

            // 5. Parallel Additive Lagged Fibonacci pseudo-random number generator
            var palf = new Palf(1);
            Console.WriteLine(@"5. Generate 10 random bytes using Parallel Additive Lagged Fibonacci pseudo-random number generator");
            var bytes = new byte[10];
            palf.NextBytes(bytes);
            for (var i = 0; i < bytes.Length; i++)
            {
                Console.Write(bytes[i] + @" ");
            }

            Console.WriteLine();
            Console.WriteLine();

            // 6. A random number generator based on the "System.Security.Cryptography.RandomNumberGenerator" class in the .NET library
            var systemCrypto = new CryptoRandomSource();
            Console.WriteLine(@"6. Generate 10 random decimal values using RNG based on the 'System.Security.Cryptography.RandomNumberGenerator'");
            for (var i = 0; i < 10; i++)
            {
                Console.Write(systemCrypto.NextDecimal().ToString("N") + @" ");
            }

            Console.WriteLine();
            Console.WriteLine();

            // 7. Wichmann-Hill’s 1982 combined multiplicative congruential generator
            var rngWh1982 = new WH1982();
            Console.WriteLine(@"7. Generate 10 random full Int32 range values using Wichmann-Hill’s 1982 combined multiplicative congruential generator");
            for (var i = 0; i < 10; i++)
            {
                Console.Write(rngWh1982.NextFullRangeInt32() + @" ");
            }

            Console.WriteLine();
            Console.WriteLine();

            // 8. Wichmann-Hill’s 2006 combined multiplicative congruential generator.
            var rngWh2006 = new WH2006();
            Console.WriteLine(@"8. Generate 10 random full Int64 range values using Wichmann-Hill’s 2006 combined multiplicative congruential generator");
            for (var i = 0; i < 10; i++)
            {
                Console.Write(rngWh2006.NextFullRangeInt32() + @" ");
            }

            Console.WriteLine();
            Console.WriteLine();

            // 9. Multiply-with-carry Xorshift pseudo random number generator
            var xorshift = new Xorshift();
            Console.WriteLine(@"9. Generate 10 random nonnegative values less than Int64.MaxValue using Multiply-with-carry Xorshift pseudo random number generator");
            for (var i = 0; i < 10; i++)
            {
                Console.Write(xorshift.NextInt64() + @" ");
            }

            Console.WriteLine();
        }