PageRenderTime 45ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/upload/includes/battle_engine/utils/Gauss.php

https://gitlab.com/maxauvy/XG_Project_v2
PHP | 128 lines | 38 code | 6 blank | 84 comment | 6 complexity | 2b01269f54ca3e4a5d9fbe950a1cbe80 MD5 | raw file
Possible License(s): GPL-3.0
  1. <?php
  2. /**
  3. * OPBE
  4. * Copyright (C) 2013 Jstar
  5. *
  6. * This file is part of OPBE.
  7. *
  8. * OPBE is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * OPBE is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License
  19. * along with OPBE. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. * @package OPBE
  22. * @author Jstar <frascafresca@gmail.com>
  23. * @copyright 2013 Jstar <frascafresca@gmail.com>
  24. * @license http://www.gnu.org/licenses/ GNU AGPLv3 License
  25. * @version beta(26-10-2013)
  26. * @link https://github.com/jstar88/opbe
  27. */
  28. class Gauss
  29. {
  30. /**
  31. * Random::getNext()
  32. * Return an random normal number
  33. * @return int
  34. */
  35. public static function getNext()
  36. {
  37. $x = (float)mt_rand() / (float)mt_getrandmax();
  38. $y = (float)mt_rand() / (float)mt_getrandmax();
  39. $u = sqrt(-2 * log($x)) * cos(2 * pi() * $y);
  40. $v = sqrt(-2 * log($x)) * sin(2 * pi() * $y);
  41. return $u;
  42. }
  43. /**
  44. * Random::getNextMs()
  45. * Generates a random number from the normal distribution with specific mean and standard deviation
  46. * @param int $m: mean
  47. * @param int $s: standard deviation
  48. * @return int
  49. */
  50. public static function getNextMs($m, $s)
  51. {
  52. return self::getNext() * $s + $m;
  53. }
  54. /**
  55. * Random::getNextMsBetween()
  56. * Generates a random number from the normal distribution with specific mean and standard deviation.
  57. * The number must be between min and max.
  58. * @param int $m: mean
  59. * @param int $s: standard deviation
  60. * @param int $min: the minimum
  61. * @param int $max: the maximum
  62. * @return int
  63. */
  64. public static function getNextMsBetween($m, $s, $min, $max)
  65. {
  66. $i = 0;
  67. if ($min > $m || $max < $m)
  68. {
  69. throw new Exception("Mean is not bounded by min and max");
  70. }
  71. while (true)
  72. {
  73. $n = self::getNextMs($m, $s);
  74. if ($n >= $min && $n <= $max)
  75. {
  76. return $n;
  77. }
  78. $i++;
  79. if ($i > 10)
  80. {
  81. return mt_rand($min, $max);
  82. }
  83. }
  84. }
  85. }
  86. /**
  87. * //--------------------------- testing -----------------------
  88. * //--------edit only these!----------
  89. * define('MEAN', 55);
  90. * define('DEV', sqrt(7));
  91. * define('SIMULATIONS', 1000);
  92. * //----------------------------------
  93. * $a = array();
  94. * for ($i = 0; $i < SIMULATIONS; $i++)
  95. * {
  96. * $a[] = Gauss::getNextMs(MEAN, DEV);
  97. * }
  98. * $l = array();
  99. * foreach ($a as $v)
  100. * {
  101. * if (isset($l[$v]))
  102. * $l[$v]++;
  103. * else
  104. * $l[$v] = 1;
  105. * }
  106. * ksort($l);
  107. * foreach ($l as $id => $v)
  108. * {
  109. * $s = '';
  110. * for ($i = 0; $i < $v; $i++)
  111. * {
  112. * $s .= '-';
  113. * }
  114. * echo $s . $id . '(' . $v . ')' . '<br>';
  115. * }
  116. */
  117. ?>