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