Guest User

Untitled

a guest
Jun 20th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.58 KB | None | 0 0
  1. /**
  2. * Generating 1.000.000 solar systems (3 iterations).
  3. * Chances for a 6 star solar system are fairly small.
  4. * Only one solar system with 6 stars out of 3 million.
  5. *
  6. * #1st
  7. * 1 Star : 732724
  8. * 2 Stars: 234559
  9. * 3 Stars: 30955
  10. * 4 Stars: 1728
  11. * 5 Stars: 34
  12. * 6 Stars: 0
  13. *
  14. * #2nd
  15. * 1 Star : 731628
  16. * 2 Stars: 235170
  17. * 3 Stars: 31506
  18. * 4 Stars: 1657
  19. * 5 Stars: 39
  20. * 6 Stars: 0
  21. *
  22. * #3rd
  23. * 1 Star : 732048
  24. * 2 Stars: 234795
  25. * 3 Stars: 31363
  26. * 4 Stars: 1754
  27. * 5 Stars: 39
  28. * 6 Stars: 1
  29. */
  30.  
  31. class Universe
  32. {
  33. /**
  34. * @var array
  35. */
  36. static protected $weights = array(
  37. 1 => 0.413019450,
  38. 2 => 0.132378029,
  39. 3 => 0.017650403,
  40. 4 => 0.000968619,
  41. 5 => 0.000018449,
  42. 6 => 0.000000071,
  43. );
  44.  
  45. /**
  46. * Randomly determine number of stars in a solar system.
  47. * Random number generation with a weight distribution.
  48. * Weights based on german lottery (% to win with X).
  49. *
  50. * @return integer The number of stars
  51. */
  52. static protected function getNumberOfStars()
  53. {
  54. $distribution = array();
  55. $total = 0;
  56.  
  57. foreach (self::$weights as $key => $weight) {
  58. $total += $weight;
  59. }
  60.  
  61. foreach (self::$weights as $key => $weight) {
  62. $distribution[$key] = $weight / $total;
  63. }
  64.  
  65. $random = rand() / getrandmax();
  66. while (true) {
  67. foreach ($distribution as $key => $weight) {
  68. if (($random -= $weight) < 0) {
  69. return $key;
  70. }
  71. }
  72. }
  73. }
  74. }
Add Comment
Please, Sign In to add comment