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