]> icculus.org git repositories - taylor/freespace2.git/blob - src/math/staticrand.cpp
Initial revision
[taylor/freespace2.git] / src / math / staticrand.cpp
1 /*
2  * $Logfile: /Freespace2/code/Math/StaticRand.cpp $
3  * $Revision$
4  * $Date$
5  * $Author$
6  *
7  * static random functions.  Return "random" number based on integer inut
8  *
9  * $Log$
10  * Revision 1.1  2002/05/03 03:28:09  root
11  * Initial revision
12  *
13  * 
14  * 2     10/07/98 10:53a Dave
15  * Initial checkin.
16  * 
17  * 1     10/07/98 10:49a Dave
18  * 
19  * 4     3/17/98 12:16a Allender
20  * asteroids in multiplayer -- minor problems with position being correct
21  * 
22  * 3     12/30/97 5:46p Lawrance
23  * Rename rnd() to rand_alt().
24  * 
25  * 2     12/30/97 4:27p Lawrance
26  * Add new rnd() function that doesn't affect rand() sequence.
27  * 
28  * 1     8/08/97 3:38p Allender
29  */
30
31 #include "staticrand.h"
32 #include "vecmat.h"
33
34 int Semirand_inited = 0;
35 int Semirand[SEMIRAND_MAX];
36
37 //      Initialize Semirand array.
38 void init_semirand()
39 {
40         int     i;
41
42         Semirand_inited = 1;
43
44         for (i=0; i<SEMIRAND_MAX; i++)
45                 Semirand[i] = (myrand() << 15) + myrand();
46 }
47
48
49 //      Return a fairly random 32 bit value given a reasonably small number.
50 int static_rand(int num)
51 {
52         int     a, b, c;
53
54         if (!Semirand_inited)
55                 init_semirand();
56
57         a = num & (SEMIRAND_MAX - 1);
58         b = (num >> SEMIRAND_MAX_LOG) & (SEMIRAND_MAX - 1);
59         c = (num >> (2 * SEMIRAND_MAX_LOG)) & (SEMIRAND_MAX - 1);
60
61         return Semirand[a] ^ Semirand[b] ^ Semirand[c];
62 }
63
64 //      Return a random value in 0.0f .. 1.0f- (ie, it will never return 1.0f).
65 float static_randf(int num)
66 {
67         int     a;
68
69         a = static_rand(num);
70
71         return (a & 0xffff) / 65536.0f;
72 }
73
74 float static_randf_range(int num, float min, float max)
75 {
76         float   rval;
77         
78         rval = static_randf(num);
79         rval = rval * (max - min) + min;
80
81         return rval;
82 }
83
84
85 void static_randvec(int num, vector *vp)
86 {
87         vp->x = static_randf(num) - 0.5f;
88         vp->y = static_randf(num+1) - 0.5f;
89         vp->z = static_randf(num+2) - 0.5f;
90
91         vm_vec_normalize_quick(vp);
92 }
93
94 /////////////////////////////////////////////////////////////////////
95 // Alternate random number generator, that doesn't affect rand() sequence
96 /////////////////////////////////////////////////////////////////////
97 #define RND_MASK        0x6000
98 #define RND_MAX 0x7fff
99 int Rnd_seed = 1;
100
101 // Seed the random number generator.  Doesn't have to be called.
102 void srand_alt(int seed)
103 {
104         Rnd_seed = seed;
105 }
106
107 // Get a random integer between 1 and RND_MAX
108 int rand_alt()
109 {
110         static int x=Rnd_seed;
111         int old_x;
112         old_x = x;
113         x >>= 1;
114         if ( old_x & 1 ) {
115                 x ^= RND_MASK;
116         }
117         return x;
118 }
119
120 // Get a random float between 0 and 1.0
121 float frand_alt()
122 {
123         int r = rand_alt();
124         return i2fl(r)/RND_MAX;
125 }
126