3 void RandomSelection_Init()
5 RandomSelection_totalweight = 0;
6 RandomSelection_chosen_ent = NULL;
7 RandomSelection_chosen_float = 0;
8 RandomSelection_chosen_string = string_null;
9 RandomSelection_best_priority = -1;
12 void RandomSelection_Add(entity e, float f, string s, vector v, float weight, float priority)
14 if (priority > RandomSelection_best_priority)
16 RandomSelection_best_priority = priority;
17 RandomSelection_chosen_ent = e;
18 RandomSelection_chosen_float = f;
19 RandomSelection_chosen_string = s;
20 RandomSelection_chosen_vec = v;
21 RandomSelection_totalweight = weight;
23 else if (priority == RandomSelection_best_priority)
25 RandomSelection_totalweight += weight;
26 if (random() * RandomSelection_totalweight <= weight)
28 RandomSelection_chosen_ent = e;
29 RandomSelection_chosen_float = f;
30 RandomSelection_chosen_string = s;
31 RandomSelection_chosen_vec = v;
36 float DistributeEvenly_amount;
37 float DistributeEvenly_totalweight;
39 void DistributeEvenly_Init(float amount, float totalweight)
41 if (DistributeEvenly_amount)
43 LOG_TRACE("DistributeEvenly_Init: UNFINISHED DISTRIBUTION (", ftos(DistributeEvenly_amount), " for ", ftos(DistributeEvenly_totalweight), " left!)");
45 if (totalweight == 0) DistributeEvenly_amount = 0;
46 else DistributeEvenly_amount = amount;
47 DistributeEvenly_totalweight = totalweight;
50 float DistributeEvenly_Get(float weight)
53 if (weight <= 0) return 0;
54 f = floor(0.5 + DistributeEvenly_amount * weight / DistributeEvenly_totalweight);
55 DistributeEvenly_totalweight -= weight;
56 DistributeEvenly_amount -= f;
60 float DistributeEvenly_GetRandomized(float weight)
63 if (weight <= 0) return 0;
64 f = floor(random() + DistributeEvenly_amount * weight / DistributeEvenly_totalweight);
65 DistributeEvenly_totalweight -= weight;
66 DistributeEvenly_amount -= f;
70 // from the GNU Scientific Library
71 float gsl_ran_gaussian_lastvalue;
72 float gsl_ran_gaussian_lastvalue_set;
73 float gsl_ran_gaussian(float sigma)
75 if (gsl_ran_gaussian_lastvalue_set)
77 gsl_ran_gaussian_lastvalue_set = 0;
78 return sigma * gsl_ran_gaussian_lastvalue;
82 float a = random() * 2 * M_PI;
83 float b = sqrt(-2 * log(random()));
84 gsl_ran_gaussian_lastvalue = cos(a) * b;
85 gsl_ran_gaussian_lastvalue_set = 1;
86 return sigma * sin(a) * b;
90 // prandom - PREDICTABLE random number generator (not seeded yet)
97 c = crc16(false, strcat(ftos(prandom_seed), ftos(prandom_seed + M_PI)));
100 #ifdef USE_PRANDOM_DEBUG
101 LOG_TRACE("RANDOM -> ", ftos(c));
104 return c / 65536; // in [0..1[
122 void psrandom(float seed)
125 #ifdef USE_PRANDOM_DEBUG
126 LOG_TRACE("SRANDOM ", ftos(seed));
130 #ifdef USE_PRANDOM_DEBUG
133 LOG_TRACE("Current random seed = ", ftos(prandom_seed));