Intro
This is based on an actual problem I recently faced while making a computer game and I thought it would make for a nice round of code-golf.
There are seven main spectral classes of star which put out varying amounts of heat. The geology of planets around a star are greatly influenced by the amount of heat received from the star, which is a factor of spectral class and distance from the star. Hence Mercury is practically molten, Neptune frozen.
The galaxy in my game is procedurally generated and randomly selecting planet types for given stars turned out to be a real 'if statement hell'!
The challenge
Your method should select one planet from a list of planet types appropriate for the class of star, based on a minimum heat threshold, a maximum heat threshold and a random number. For simplicity this challenge will only use a class G star, just like our sun.
Inputs
An integer heat
in the range 4 to 11 representing the amount of heat received by the planet from the star.
Variables
This table shows the possible planets based on heat
. Your method should first narrow the available choices based on the heat min and heat max, heat
should fall on or between the two. E.g. with a heat of 10 passed in the only choices would be Desert, Iron and Lava.
Planet type Heat min Heat max Random Chance
Gas Giant 4 9 15
Ice 4 6 10
Ice Giant 4 6 10
Gaia class 5 7 10
Dense Atmosphere 7 9 10
Desert 7 10 25
Iron 7 10 14
Lava 10 11 6
Next, the probability of a planet (in the remaining choices) being chosen is its random chances divided by the sum of the random chances of all the choices.
In the above example, the probability of Iron being chosen is 14/(25+14+6)
.
Output
Return the planet type as a string.
Do the best you can to avoid logic arrowheads. Shortest code wins, points all round for creativity. Happy golfing!