*EDITOR’S NOTE: This is part of a series, start here.*

*ADDITIONAL EDITOR’S NOTE: Due to the presence of tables and code samples, this page may behave strangely on mobile devices. Desktop viewing recommended.*

Somewhere a few posts ago I was thinking about how to define planets, but I skipped a step in the star/planet process. The number and orbital distances of planets have to be decided at the star level because they have to be decided for the whole system and not on a planet by planet basis. Deciding on the statistics of what this looks like is actually a bit of a balancing act between “realistic” and “plausible”. I guess that’s *always* true when developing any kind of fictional universe, really. In this case, thanks to the Kepler data, we have a reasonably realistic idea (so far as we know) of the number and types of planets around a random sampling of stars. I could just implement those stats and be done, but I probably want to tweak that a bit to make for more interesting gameplay. So not totally realistic, but *plausible*.

Here’s what I derived from the data in the Wikipedia article:

#### Planet Count

Planets per star | Number of stars | Fraction | Int Fraction |
---|---|---|---|

8 | 2 | 0.003072196621 | 3 |

7 | 3 | 0.004608294931 | 4 |

6 | 4 | 0.006144393241 | 6 |

5 | 15 | 0.02304147465 | 23 |

4 | 50 | 0.07680491551 | 76 |

3 | 127 | 0.1950844854 | 195 |

2 | 450 | 0.6912442396 | 691 |

Somehow I’ve never really had to calculate probabilities with any real degree of precision. I’m not sure what the “correct” way is, but here’s how I do it. In this case I summed the “number of stars” column and got 651. Then for each row, I figured out what fraction of the total that number of stars would be, just by putting the number of stars for that row over the total, so for line one there, it’s ^{2}⁄_{651}, line two is ^{3}⁄_{651} on up to the last row, which is ^{450}⁄_{651}. I decide the number of digits I care about (in general) by taking the smallest fraction and going to the first significant figure after the decimal point. In this case, it’s 3. To get to 3 from 0.003, you just multiply that by 1000, which gives me what I call my “int fraction”. I do the same for the rest of the numbers, then I can calculate probabilities with an algorithm like:

```
// Gets a pseudorandom number between 0 and 651, our total
const int p = hasher->get_int(str_hash, 0, 651);
if (p < 3) {
planet_count = 8;
} else if (p < 4) {
planet_count = 7;
} else if (p < 6) {
planet_count = 6;
} else if (p < 23) {
planet_count = 5;
} else if (p < 76) {
planet_count = 4;
} else if (p < 195) {
planet_count = 3;
} else {
planet_count = 2;
}
```

All we’re doing here is determining where `p`

falls. If it’s less than 3, we’re at the first planet count, if not, check if it’s less than the next amount, and so on. It takes a little longer to set up than calculating the whole thing with decimal percentages, but it’s a lot less code in the end and it’s a lot more efficient. Because these are functionally “magic numbers”, it’s important to document where they came from in the code, so the next person (me) doesn’t have to figure out what you did.