#include #include bool random_select(double weight, double *tot_weight) { *tot_weight += weight; if (weight == 0) return false; return pseudorand_double() <= weight / *tot_weight; }