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.2 2002/06/09 04:41:22 relnev
19 * added copyright header
21 * Revision 1.1.1.1 2002/05/03 03:28:09 root
25 * 2 10/07/98 10:53a Dave
28 * 1 10/07/98 10:49a Dave
30 * 4 3/17/98 12:16a Allender
31 * asteroids in multiplayer -- minor problems with position being correct
33 * 3 12/30/97 5:46p Lawrance
34 * Rename rnd() to rand_alt().
36 * 2 12/30/97 4:27p Lawrance
37 * Add new rnd() function that doesn't affect rand() sequence.
39 * 1 8/08/97 3:38p Allender
42 #include "staticrand.h"
45 int Semirand_inited = 0;
46 int Semirand[SEMIRAND_MAX];
48 // Initialize Semirand array.
55 for (i=0; i<SEMIRAND_MAX; i++)
56 Semirand[i] = (myrand() << 15) + myrand();
60 // Return a fairly random 32 bit value given a reasonably small number.
61 int static_rand(int num)
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);
72 return Semirand[a] ^ Semirand[b] ^ Semirand[c];
75 // Return a random value in 0.0f .. 1.0f- (ie, it will never return 1.0f).
76 float static_randf(int num)
82 return (a & 0xffff) / 65536.0f;
85 float static_randf_range(int num, float min, float max)
89 rval = static_randf(num);
90 rval = rval * (max - min) + min;
96 void static_randvec(int num, vector *vp)
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;
102 vm_vec_normalize_quick(vp);
105 /////////////////////////////////////////////////////////////////////
106 // Alternate random number generator, that doesn't affect rand() sequence
107 /////////////////////////////////////////////////////////////////////
108 #define RND_MASK 0x6000
109 #define RND_MAX 0x7fff
112 // Seed the random number generator. Doesn't have to be called.
113 void srand_alt(int seed)
118 // Get a random integer between 1 and RND_MAX
121 static int x=Rnd_seed;
131 // Get a random float between 0 and 1.0
135 return i2fl(r)/RND_MAX;