Starshatter_Open
Open source Starshatter engine
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
Random.cpp
Go to the documentation of this file.
1 /* Project nGenEx
2  Destroyer Studios LLC
3  Copyright © 1997-2004. All Rights Reserved.
4 
5  SUBSYSTEM: nGenEx.lib
6  FILE: Random.cpp
7  AUTHOR: John DiCamillo
8 
9 
10  OVERVIEW
11  ========
12  Utility functions for generating random numbers and locations.
13 */
14 
15 #include "MemDebug.h"
16 #include "Random.h"
17 
18 // +----------------------------------------------------------------------+
19 
20 void RandomInit()
21 {
22  srand(timeGetTime());
23 }
24 
25 // +----------------------------------------------------------------------+
26 
28 {
29  Point p = Point(rand() - 16384, rand() - 16384, 0);
30  p.Normalize();
31  return p;
32 }
33 
34 // +----------------------------------------------------------------------+
35 
37 {
38  Point p = Point(rand() - 16384, rand() - 16384, 0);
39  p.Normalize();
40  p *= 15e3 + rand()/3;
41  return p;
42 }
43 
44 // +----------------------------------------------------------------------+
45 
46 Vec3 RandomVector(double radius)
47 {
48  Vec3 v = Vec3(rand() - 16384, rand() - 16384, rand() - 16384);
49  v.Normalize();
50 
51  if (radius > 0)
52  v *= (float) radius;
53  else
54  v *= (float) Random(radius/3, radius);
55 
56  return v;
57 }
58 
59 // +----------------------------------------------------------------------+
60 
61 double Random(double min, double max)
62 {
63  double delta = max - min;
64  double r = delta * rand() / 32768.0;
65 
66  return min + r;
67 }
68 
69 // +----------------------------------------------------------------------+
70 
72 {
73  static int index = 0;
74  static int table[16] = { 0, 9, 4, 7, 14, 11, 2, 12, 1, 5, 13, 8, 6, 10, 3, 15 };
75 
76  int r = 1 + ((rand() & 0x0700) >> 8);
77  index += r;
78  if (index > 1e7) index = 0;
79  return table[index % 16];
80 }
81 
82 // +----------------------------------------------------------------------+
83 
84 bool RandomChance(int wins, int tries)
85 {
86  double fraction = 256.0 * wins / tries;
87  double r = (rand() >> 4) & 0xFF;
88 
89  return r < fraction;
90 }
91 
92 // +----------------------------------------------------------------------+
93 
94 int RandomSequence(int current, int range)
95 {
96  if (range > 1) {
97  int step = (int) Random(1, range-1);
98  return (current + step) % range;
99  }
100 
101  return current;
102 }
103 
104 // +----------------------------------------------------------------------+
105 
106 int RandomShuffle(int count)
107 {
108  static int set_size = -1;
109  static BYTE set[256];
110  static int index = -1;
111 
112  if (count < 0 || count > 250)
113  return 0;
114 
115  if (set_size != count) {
116  set_size = count;
117  index = -1;
118  }
119 
120  // need to reshuffle
121  if (index < 0 || index > set_size-1) {
122  // set up the deck
123  int tmp[256];
124  for (int i = 0; i < 256; i++)
125  tmp[i] = i;
126 
127  // shuffle the cards
128  for (int i = 0; i < set_size; i++) {
129  int n = (int) Random(0, set_size);
130  int tries = set_size;
131  while (tmp[n] < 0 && tries--) {
132  n = (n+1) % set_size;
133  }
134 
135  if (tmp[n] >= 0) {
136  set[i] = tmp[n];
137  tmp[n] = -1;
138  }
139  else {
140  set[i] = 0;
141  }
142  }
143 
144  index = 0;
145  }
146 
147  return set[index++];
148 }