4 void RandomSelection_Init()
6 RandomSelection_totalweight = 0;
7 RandomSelection_chosen_ent = NULL;
8 RandomSelection_chosen_float = 0;
9 RandomSelection_chosen_string = string_null;
10 RandomSelection_best_priority = -1;
14 void RandomSelection_Add(entity e, float f, string s, vector v, float weight, float priority)
16 if (priority > RandomSelection_best_priority)
18 RandomSelection_best_priority = priority;
19 RandomSelection_chosen_ent = e;
20 RandomSelection_chosen_float = f;
21 RandomSelection_chosen_string = s;
22 RandomSelection_chosen_vec = v;
23 RandomSelection_totalweight = weight;
25 else if (priority == RandomSelection_best_priority)
27 RandomSelection_totalweight += weight;
28 if (random() * RandomSelection_totalweight <= weight)
30 RandomSelection_chosen_ent = e;
31 RandomSelection_chosen_float = f;
32 RandomSelection_chosen_string = s;
33 RandomSelection_chosen_vec = v;
38 float DistributeEvenly_amount;
39 float DistributeEvenly_totalweight;
42 void DistributeEvenly_Init(float amount, float totalweight)
44 if (DistributeEvenly_amount)
46 LOG_TRACE("DistributeEvenly_Init: UNFINISHED DISTRIBUTION (", ftos(DistributeEvenly_amount), " for ", ftos(DistributeEvenly_totalweight), " left!)");
48 if (totalweight == 0) DistributeEvenly_amount = 0;
49 else DistributeEvenly_amount = amount;
50 DistributeEvenly_totalweight = totalweight;
54 float DistributeEvenly_Get(float weight)
57 if (weight <= 0) return 0;
58 f = floor(0.5 + DistributeEvenly_amount * weight / DistributeEvenly_totalweight);
59 DistributeEvenly_totalweight -= weight;
60 DistributeEvenly_amount -= f;
65 float DistributeEvenly_GetRandomized(float weight)
68 if (weight <= 0) return 0;
69 f = floor(random() + DistributeEvenly_amount * weight / DistributeEvenly_totalweight);
70 DistributeEvenly_totalweight -= weight;
71 DistributeEvenly_amount -= f;
75 // from the GNU Scientific Library
76 float gsl_ran_gaussian_lastvalue;
77 float gsl_ran_gaussian_lastvalue_set;
79 float gsl_ran_gaussian(float sigma)
81 if (gsl_ran_gaussian_lastvalue_set)
83 gsl_ran_gaussian_lastvalue_set = 0;
84 return sigma * gsl_ran_gaussian_lastvalue;
88 float a = random() * 2 * M_PI;
89 float b = sqrt(-2 * log(random()));
90 gsl_ran_gaussian_lastvalue = cos(a) * b;
91 gsl_ran_gaussian_lastvalue_set = 1;
92 return sigma * sin(a) * b;
96 // prandom - PREDICTABLE random number generator (not seeded yet)
103 c = crc16(false, strcat(ftos(prandom_seed), ftos(prandom_seed + M_PI)));
106 #ifdef USE_PRANDOM_DEBUG
107 LOG_TRACE("RANDOM -> ", ftos(c));
110 return c / 65536; // in [0..1[
128 void psrandom(float seed)
131 #ifdef USE_PRANDOM_DEBUG
132 LOG_TRACE("SRANDOM ", ftos(seed));
136 #ifdef USE_PRANDOM_DEBUG
139 LOG_TRACE("Current random seed = ", ftos(prandom_seed));