Esempio n. 1
0
 public static Complex CreateComplex()
 {
     using (var random = new SecureRandom())
     {
         return(new Complex(random.NextDouble() * 100.0,
                            random.NextDouble() * 100.0));
     }
 }
        public void Random_Next_Double()
        {
            var result = dice.NextDouble();

            TestOut("Result: {0}", result);
            bool ok = (result >= double.MinValue) && (result <= double.MaxValue);

            Assert.IsTrue(ok, "Not a valid range");
        }
Esempio n. 3
0
        private IEnumerable <Chromosome <T> > DoCrossover(Population <T> generationPopulation,
                                                          SecureRandom random, int childCount)
        {
            const int ParentCount       = 2;
            var       RouletteSelection = (uint)Math.Ceiling((double)generationPopulation.Chromosomes.Count * 0.05);

            var crossovers = new ConcurrentBag <Chromosome <T> >();

            var tasks = new Task[this.Parameters.TaskCount];

            for (var c = 0; c < this.Parameters.TaskCount; c++)
            {
                tasks[c] = Task.Factory.StartNew(() =>
                {
                    do
                    {
                        var parents = new List <Chromosome <T> >();
                        var indexes = random.GetInt32Values(RouletteSelection,
                                                            new Range <int>(0, generationPopulation.Chromosomes.Count), ValueGeneration.UniqueValuesOnly);

                        for (var i = 0; i < ParentCount; i++)
                        {
                            var parent = (from chromosome in
                                          (from index in indexes
                                           select generationPopulation.Chromosomes[index])
                                          orderby chromosome.Fitness descending
                                          select chromosome).Take(1).First();

                            parents.Add(parent);
                        }

                        var children = random.NextDouble() < this.Parameters.CrossoverProbability ?
                                       this.Parameters.Crossover(parents.AsReadOnly()) :
                                       new List <T>(from parent in parents
                                                    select this.Parameters.Copy(parent.Value)).AsReadOnly();

                        foreach (var child in children)
                        {
                            var mutatedChild = this.Parameters.Mutator(child);

                            if (mutatedChild != null)
                            {
                                crossovers.Add(new Chromosome <T>(
                                                   mutatedChild, this.Parameters.FitnessEvaluator(mutatedChild)));
                            }
                        }
                    } while (crossovers.Count < childCount);
                });
            }

            Task.WaitAll(tasks);
            return(crossovers.AsEnumerable());
        }
Esempio n. 4
0
        public void TestDoubles()
        {
            double sum = 0.0;

            for (int i = 0; i < 100; ++i)
            {
                sum += RNG.NextDouble();
            }
            Assert.Equal(5208, Counter.BitsRead);
            Assert.True(sum < 60.0);
            Assert.True(sum > 40.0);
        }
Esempio n. 5
0
        public void NextDouble_should_always_return_a_double_between_lesser_than_1()
        {
            var random  = new SecureRandom();
            int counter = 0;

            while (counter < 1000)
            {
                var val = random.NextDouble();
                Console.WriteLine("value: {0}", val);
                Assert.IsTrue(val <= 1.0 && val >= 0.0);
                counter++;
            }
        }
        public string Mutator(string chromosome)
        {
            chromosome.CheckParameterForNull("chromosome");

            var mutatedChromosome = new StringBuilder();

            using (var random = new SecureRandom())
            {
                foreach (char allele in chromosome)
                {
                    if (random.NextDouble() < this.MutationProbability)
                    {
                        mutatedChromosome.Append(allele == '1' ? '0' : '1');
                    }
                    else
                    {
                        mutatedChromosome.Append(allele);
                    }
                }
            }

            return(mutatedChromosome.ToString());
        }
Esempio n. 7
0
 public static SqlDouble GetRandomDouble()
 {
     return(_rnd.NextDouble());
 }
 /// <summary>
 /// Gets the next random <see langword="double"/> from the <see cref="SecureRandom"/>.
 /// </summary>
 /// <returns> The random <see langword="double"/>. </returns>
 public override double NextDouble() => secureRandom.NextDouble();
Esempio n. 9
0
 public static float RandomFloat(float max)
 {
     return((float)(Rnd.NextDouble() * max));
 }
Esempio n. 10
0
 public void GenerateDoubles()
 {
     using (var random = new SecureRandom())
     {
         for (var i = 0; i < 500000; i++)
         {
             var d = random.NextDouble();
             Assert.IsTrue(d >= 0.0);
             Assert.IsTrue(d < 1.0);
         }
     }
 }
Esempio n. 11
0
        public void GenerateDoubleAfterDisposing()
        {
            SecureRandom random = null;

            using (random = new SecureRandom())
            {
            }

            random.NextDouble();
        }
Esempio n. 12
0
        private void SecRandTest()
        {
            using (SecureRandom rnd = new SecureRandom())
            {
                double x1 = 0.0;
                for (int i = 0; i < 1000; i++)
                {
                    x1 = rnd.NextDouble();
                    if (x1 > 1.0)
                    {
                        throw new Exception("SecureRandom: NextDouble returned a value outside of the expected range.");
                    }
                }

                short x2 = 0;
                for (int i = 0; i < 1000; i++)
                {
                    x2 = rnd.NextInt16(1, 6);
                    if (x2 > 6)
                    {
                        throw new Exception("SecureRandom: NextInt16 returned a value outside of the expected range.");
                    }
                    if (x2 < 1)
                    {
                        throw new Exception("SecureRandom: NextInt16 returned a value outside of the expected range.");
                    }
                }

                ushort x3 = 0;
                for (int i = 0; i < 1000; i++)
                {
                    x3 = rnd.NextUInt16(1, 52);
                    if (x3 > 52)
                    {
                        throw new Exception("SecureRandom: NextUInt16 returned a value outside of the expected range.");
                    }
                    if (x3 < 1)
                    {
                        throw new Exception("SecureRandom: NextUInt16 returned a value outside of the expected range.");
                    }
                }

                int x4 = 0;
                for (int i = 0; i < 1000; i++)
                {
                    x4 = rnd.NextInt32(3371, 16777216);
                    if (x4 > 16777216)
                    {
                        throw new Exception("SecureRandom: NextInt32 returned a value outside of the expected range.");
                    }
                    if (x4 < 3371)
                    {
                        throw new Exception("SecureRandom: NextInt32 returned a value outside of the expected range.");
                    }
                }

                uint x5 = 0;
                for (int i = 0; i < 1000; i++)
                {
                    x5 = rnd.NextUInt32(77721, 777216);
                    if (x5 > 777216)
                    {
                        throw new Exception("SecureRandom: NextUInt32 returned a value outside of the expected range.");
                    }
                    if (x5 < 77721)
                    {
                        throw new Exception("SecureRandom: NextUInt32 returned a value outside of the expected range.");
                    }
                }

                long x6 = 0;
                for (int i = 0; i < 1000; i++)
                {
                    x6 = rnd.NextInt64(2814749767, 281474976710653);
                    if (x6 > 281474976710656)
                    {
                        throw new Exception("SecureRandom: NextInt64 returned a value outside of the expected range.");
                    }
                    if (x6 < 2814749767)
                    {
                        throw new Exception("SecureRandom: NextInt64 returned a value outside of the expected range.");
                    }
                }

                ulong x7 = 0;
                for (int i = 0; i < 1000; i++)
                {
                    x7 = rnd.NextUInt64(5759403792, 72057594037927934);
                    if (x7 > 72057594037927936)
                    {
                        throw new Exception("SecureRandom: NextUInt64 returned a value outside of the expected range.");
                    }
                    if (x7 < 5759403792)
                    {
                        throw new Exception("SecureRandom: NextUInt64 returned a value outside of the expected range.");
                    }
                }
            }
        }
Esempio n. 13
0
        public void DoubleTest()
        {
            var nextD = _random.NextDouble();

            Assert.AreNotEqual(0.0, nextD);
        }