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, 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_totalweight = weight;
22 else if (priority == RandomSelection_best_priority)
24 RandomSelection_totalweight += weight;
25 if (random() * RandomSelection_totalweight <= weight)
27 RandomSelection_chosen_ent = e;
28 RandomSelection_chosen_float = f;
29 RandomSelection_chosen_string = s;
34 float DistributeEvenly_amount;
35 float DistributeEvenly_totalweight;
37 void DistributeEvenly_Init(float amount, float totalweight)
39 if (DistributeEvenly_amount)
41 LOG_TRACE("DistributeEvenly_Init: UNFINISHED DISTRIBUTION (", ftos(DistributeEvenly_amount), " for ");
42 LOG_TRACE(ftos(DistributeEvenly_totalweight), " left!)\n");
44 if (totalweight == 0) DistributeEvenly_amount = 0;
45 else DistributeEvenly_amount = amount;
46 DistributeEvenly_totalweight = totalweight;
49 float DistributeEvenly_Get(float weight)
52 if (weight <= 0) return 0;
53 f = floor(0.5 + DistributeEvenly_amount * weight / DistributeEvenly_totalweight);
54 DistributeEvenly_totalweight -= weight;
55 DistributeEvenly_amount -= f;
59 float DistributeEvenly_GetRandomized(float weight)
62 if (weight <= 0) return 0;
63 f = floor(random() + DistributeEvenly_amount * weight / DistributeEvenly_totalweight);
64 DistributeEvenly_totalweight -= weight;
65 DistributeEvenly_amount -= f;
69 // from the GNU Scientific Library
70 float gsl_ran_gaussian_lastvalue;
71 float gsl_ran_gaussian_lastvalue_set;
72 float gsl_ran_gaussian(float sigma)
74 if (gsl_ran_gaussian_lastvalue_set)
76 gsl_ran_gaussian_lastvalue_set = 0;
77 return sigma * gsl_ran_gaussian_lastvalue;
81 float a = random() * 2 * M_PI;
82 float b = sqrt(-2 * log(random()));
83 gsl_ran_gaussian_lastvalue = cos(a) * b;
84 gsl_ran_gaussian_lastvalue_set = 1;
85 return sigma * sin(a) * b;
89 // prandom - PREDICTABLE random number generator (not seeded yet)
96 c = crc16(false, strcat(ftos(prandom_seed), ftos(prandom_seed + M_PI)));
99 #ifdef USE_PRANDOM_DEBUG
100 LOG_TRACE("RANDOM -> ", ftos(c), "\n");
103 return c / 65536; // in [0..1[
121 void psrandom(float seed)
124 #ifdef USE_PRANDOM_DEBUG
125 LOG_TRACE("SRANDOM ", ftos(seed), "\n");
129 #ifdef USE_PRANDOM_DEBUG
132 LOG_TRACE("Current random seed = ", ftos(prandom_seed), "\n");