Skip to content

Instantly share code, notes, and snippets.

@immutef
Created November 6, 2010 19:55
Show Gist options
  • Save immutef/665666 to your computer and use it in GitHub Desktop.
Save immutef/665666 to your computer and use it in GitHub Desktop.
/**
* Generating 1.000.000 solar systems (3 iterations).
* Chances for a 6 star solar system are fairly small.
* Only one solar system with 6 stars out of 3 million.
*
* #1st
* 1 Star : 732724
* 2 Stars: 234559
* 3 Stars: 30955
* 4 Stars: 1728
* 5 Stars: 34
* 6 Stars: 0
*
* #2nd
* 1 Star : 731628
* 2 Stars: 235170
* 3 Stars: 31506
* 4 Stars: 1657
* 5 Stars: 39
* 6 Stars: 0
*
* #3rd
* 1 Star : 732048
* 2 Stars: 234795
* 3 Stars: 31363
* 4 Stars: 1754
* 5 Stars: 39
* 6 Stars: 1
*/
class Universe
{
/**
* @var array
*/
static protected $weights = array(
1 => 0.413019450,
2 => 0.132378029,
3 => 0.017650403,
4 => 0.000968619,
5 => 0.000018449,
6 => 0.000000071,
);
/**
* Randomly determine number of stars in a solar system.
* Random number generation with a weight distribution.
* Weights based on german lottery (% to win with X).
*
* @return integer The number of stars
*/
static protected function getNumberOfStars()
{
$distribution = array();
$total = 0;
foreach (self::$weights as $key => $weight) {
$total += $weight;
}
foreach (self::$weights as $key => $weight) {
$distribution[$key] = $weight / $total;
}
$random = rand() / getrandmax();
while (true) {
foreach ($distribution as $key => $weight) {
if (($random -= $weight) < 0) {
return $key;
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment