the last one, I think...
Anyways, here's a short summary: I'm rewriting the star generator, because in order to make the pathfinding work I have to have a possibility to quickly check what kind of stars are located in a region without creating the entire region first.
Before, every Star would generate a random number, and then assign a mass to it based on that random number and some probabilities derived from the initial mass function. In other words, I know the probabilities that a star would come out as 90 solar masses, or 60, or whatever, but no way of knowing wheather that region would contain any stars of higher mass ranges without generating the entire region first.
Now I'm switching it around. The generator generates a random number between 0 and 1, and then calculates the probability of at least one star of mass 90 or above occuring in that region. If the random number is lower than that probability, the algorithm goes on and calculates the probability that there be at least two stars above 90 in this region. It goes on as long as the probability is larger than the random number, and quits as soon as the probability falls below. The generator then knows how many stars of 90 or more solar masses it has to generate in this region. Repeat for other mass steps to generate the whole region.
So if the pathfinding is checking a region for a star of a certain mass (because jump distances are depending on mass), it can easily check wheather there are stars in the mass region it is looking for (if it finds one it still has to walk it through the evolution cycle to see if it is still that mass at the time, but that doesn't take too long if it has only to be done with a few stars).
The problem was to find an equation that allows me to calculate the probability that at least x stars are formed in a region containing n stars. As I said, the probability p that a star turns out a certain mass I already had.
That equation was provided by my father in law, and looks as follows:
pE = n! / (x! * (n - x)!) * q^(n-x) * p^x;
pT = Sum[pE(n,1)...pE(n,x)];
pE is the probability that exactly x out of n stars will be in that mass range.
pT is the probability that AT LEAST x out of n stars will be in that mass range, so the result I need in the end.
p is the probability of getting a star in that mass range with n = 1, q is 1 - p.
The whole thing is working very decently, up to the point where n! is too big for any data type to handle. As I said, the most simple solution seems to be to cut n into smaller pieces and run the algorithm multiple times. A bit of performance loss, but a sure-fire way.
However, If there is a way to simplify n! / (x! * (n-x)! so n! doesn't have to be calculated in the first place, that would solve the problem more efficiently.
n has a maximum value of 1000, by the way.
---------- Post added at 05:50 PM ---------- Previous post was at 05:33 PM ----------
I just ran the checks, it turns out that the maximum value for n is only 170. n! is then 7.2574156153079940e+306. At n = 171, even a long double returns #INF. That would mean cutting the process up into quite small pieces, so that the algorithm has to run 6 times when n = 1000. That'd be a bit more of a performance drop than I expected, but still doable, since I can run them parallel (i.e. I don't have to calculate all the probabilities 6 times, I only have to generate six random numbers. It's a bit of a bummer none the less, so if there's any way to circumvent calculating n!, it would be a much better solution...)