Use a genetic algorithm to evolve populations of bit strings.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

87 lines
1.4 KiB

  1. //
  2. #include "BitEvolver/Random.h"
  3. //
  4. #include <random>
  5. #include <chrono>
  6. //
  7. namespace BitEvolver
  8. {
  9. //
  10. Random::Random()
  11. {
  12. //
  13. this->InitializeGenerators();
  14. }
  15. //
  16. int Random::GetInt(int min, int max)
  17. {
  18. //
  19. std::uniform_int_distribution<int> distribution(min, max);
  20. //
  21. int the_int = distribution(this->generator_mersenne_twister);
  22. return the_int;
  23. }
  24. //
  25. double Random::GetDouble(double min, double max)
  26. {
  27. //
  28. std::uniform_real_distribution<double> distribution(min, max);
  29. //
  30. double the_double = distribution(this->generator_mersenne_twister);
  31. return the_double;
  32. }
  33. //
  34. double Random::GetNormal(double mean, double standard_deviation)
  35. {
  36. //
  37. std::normal_distribution<double> distribution(mean, standard_deviation);
  38. double d = distribution(this->generator_mersenne_twister);
  39. return d;
  40. }
  41. //
  42. bool Random::RollBool(double chance)
  43. {
  44. //
  45. double d = this->GetDouble(0, 1);
  46. if ( d <= chance ) {
  47. return true;
  48. }
  49. return false;
  50. }
  51. //
  52. void Random::InitializeGenerators()
  53. {
  54. // Mostly taken from
  55. // http://www.cplusplus.com/reference/random/mersenne_twister_engine/seed/
  56. typedef std::chrono::high_resolution_clock myclock;
  57. //
  58. myclock::time_point beginning = myclock::now();
  59. myclock::duration d = myclock::now() - beginning;
  60. unsigned seed = d.count();
  61. // Seed our internal generator
  62. this->generator_mersenne_twister.seed(seed);
  63. }
  64. };