2 * Copyright (C) Volition, Inc. 1999. All rights reserved.
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
10 * $Logfile: /Freespace2/code/Math/StaticRand.cpp $
15 * static random functions. Return "random" number based on integer inut
18 * Revision 1.3 2002/06/17 06:33:09 relnev
19 * ryan's struct patch for gcc 2.95
21 * Revision 1.2 2002/06/09 04:41:22 relnev
22 * added copyright header
24 * Revision 1.1.1.1 2002/05/03 03:28:09 root
28 * 2 10/07/98 10:53a Dave
31 * 1 10/07/98 10:49a Dave
33 * 4 3/17/98 12:16a Allender
34 * asteroids in multiplayer -- minor problems with position being correct
36 * 3 12/30/97 5:46p Lawrance
37 * Rename rnd() to rand_alt().
39 * 2 12/30/97 4:27p Lawrance
40 * Add new rnd() function that doesn't affect rand() sequence.
42 * 1 8/08/97 3:38p Allender
45 #include "staticrand.h"
48 int Semirand_inited = 0;
49 int Semirand[SEMIRAND_MAX];
51 // Initialize Semirand array.
58 for (i=0; i<SEMIRAND_MAX; i++)
59 Semirand[i] = (myrand() << 15) + myrand();
63 // Return a fairly random 32 bit value given a reasonably small number.
64 int static_rand(int num)
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);
75 return Semirand[a] ^ Semirand[b] ^ Semirand[c];
78 // Return a random value in 0.0f .. 1.0f- (ie, it will never return 1.0f).
79 float static_randf(int num)
85 return (a & 0xffff) / 65536.0f;
88 float static_randf_range(int num, float min, float max)
92 rval = static_randf(num);
93 rval = rval * (max - min) + min;
99 void static_randvec(int num, vector *vp)
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;
105 vm_vec_normalize_quick(vp);
108 /////////////////////////////////////////////////////////////////////
109 // Alternate random number generator, that doesn't affect rand() sequence
110 /////////////////////////////////////////////////////////////////////
111 #define RND_MASK 0x6000
112 #define RND_MAX 0x7fff
115 // Seed the random number generator. Doesn't have to be called.
116 void srand_alt(int seed)
121 // Get a random integer between 1 and RND_MAX
124 static int x=Rnd_seed;
134 // Get a random float between 0 and 1.0
138 return i2fl(r)/RND_MAX;