This repository has been archived by the owner on Jan 26, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRandSeqGenerator.cpp
executable file
·131 lines (114 loc) · 2.71 KB
/
RandSeqGenerator.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include <stdio.h>
#include "RandSeqGenerator.h"
const long IA = 16807;
const long IM = 2147483647;
const float AM = 1.0f / (float) IM;
const long IQ = 127773;
const long IR = 2836;
const int NTAB = 32;
const long NDIV = 1 + (IM - 1) / NTAB;
const double EPS = 1.2e-7;
const double RNMX = 1.0f - EPS;
void PositionPowerUps(int *powerUpOrder)
{
static long initSeed = -IM;
float randFloat;
int randNum;
bool zeroSelected = false, oneSelected = false, twoSelected = false, threeSelected = false, fourSelected = false;
static bool indexStored; // If value is going to be stored.
int i = 0; // Position index.
while(!zeroSelected || !oneSelected || !twoSelected || !threeSelected || !fourSelected)
{
randFloat = RandGen(&initSeed);
randNum = (int)(1e6 * randFloat) % 5;
switch(randNum)
{
case 0:
if(!zeroSelected)
{
powerUpOrder[i] = randNum;
indexStored = true;
}
else indexStored = false; // Not stored, so go through iteration again.
zeroSelected = true; // We know that this value has been selected and either stored or not.
break;
case 1:
if(!oneSelected)
{
powerUpOrder[i] = randNum;
indexStored = true;
}
else indexStored = false;
oneSelected = true;
break;
case 2:
if(!twoSelected)
{
powerUpOrder[i] = randNum;
indexStored = true;
}
else indexStored = false;
twoSelected = true;
break;
case 3:
if(!threeSelected)
{
powerUpOrder[i] = randNum;
indexStored = true;
}
else indexStored = false;
threeSelected = true;
break;
case 4:
if(!fourSelected)
{
powerUpOrder[i] = randNum;
indexStored = true;
}
else indexStored = false;
fourSelected = true;
break;
}
if(indexStored)
i++; // Increment the index if successful.
else
indexStored = true; // If unsuccessful storage, let index remain at current value and set indexStored to receive next value.
}
}
float RandGen(long *seed)
// Function borrowed from Numerical Recipes in C, Chapter 7.
{
int j;
long k;
static long iy = 0;
static long iv[NTAB];
float temp;
if(*seed <= 0 || !iy)
{
if(-*seed < 1)
*seed = 1;
else
*seed = -*seed;
for(j = NTAB + 7; j >= 0; j--)
{
k = *seed / IQ;
*seed = IA * (*seed - k * IQ) - IR * k;
if(*seed < 0)
*seed += IM;
if(j < NTAB)
iv[j] = *seed;
}
iy = iv[0];
}
k = *seed / IQ;
*seed = IA * (*seed - k * IQ) - IR * k;
if(*seed < 0)
*seed += IM;
j = iy / NDIV;
iy = iv[j];
iv[j] = *seed;
if((temp = AM * iy) > RNMX)
return (float) RNMX;
else
return temp;
}