private int[] ChooseParents(int[] weights, int sum) { var result = new int[2]; int[] value = { 0, 0 }; if (sum == 0) { return(value); } if (CanSelfReproduce) { value = Random.Integers(sum).Take(2).ToArray(); } else { while (value[0] == value[1]) //prevent parent from reproducing with itself { Random.Integers(sum).Take(2).ToArray(); } } bool first = true, second = true; for (int i = 0; i < weights.Length; i++) { if (first && weights[i] > value[0]) { result[0] = i; first = false; } if (second && weights[i] > value[1]) { result[1] = i; second = false; } if (!second && !first) { break; } } return(result); }
public static void Main() { // 1) Use TRandom to generate a few random numbers - via IGenerator methods. Console.WriteLine("TRandom in action, used as an IGenerator"); var trandom = new TRandom(); Console.WriteLine(trandom.Next() - trandom.Next(5) + trandom.Next(3, 5)); Console.WriteLine(trandom.NextDouble() * trandom.NextDouble(5.5) * trandom.NextDouble(10.1, 21.9)); Console.WriteLine(trandom.NextBoolean()); Console.WriteLine(); // 2) Use TRandom to generate a few random numbers - via extension methods. Console.WriteLine("TRandom in action, used as an IGenerator augmented with extension methods"); Console.WriteLine(string.Join(", ", trandom.Integers().Take(10))); Console.WriteLine(string.Join(", ", trandom.Doubles().Take(10))); Console.WriteLine(string.Join(", ", trandom.Booleans().Take(10))); Console.WriteLine(); // 3) Use TRandom to generate a few distributed numbers. Console.WriteLine("TRandom in action, used as to get distributed numbers"); Console.WriteLine(trandom.Normal(1.0, 0.1)); Console.WriteLine(string.Join(", ", trandom.NormalSamples(1.0, 0.1).Take(20))); Console.WriteLine(trandom.Poisson(5)); Console.WriteLine(string.Join(", ", trandom.PoissonSamples(5).Take(20))); Console.WriteLine(); // 4) There are many generators available - XorShift128 is the default. var alf = new ALFGenerator(TMath.Seed()); var nr3 = new NR3Generator(); var std = new StandardGenerator(127); // 5) You can also use distribution directly, even with custom generators. Console.WriteLine("Showcase of some distributions"); Console.WriteLine("Static sample for Normal: " + NormalDistribution.Sample(alf, 1.0, 0.1)); Console.WriteLine("New instance for Normal: " + new NormalDistribution(1.0, 0.1).NextDouble()); Console.WriteLine(); }