Game Development Reference
In-Depth Information
The next step in the above function is slightly different than what we have done be-
fore. Rather than hold a separate value for the total size to determine what the edge
is, we use the edge of the previous bucket. By adding the size of the current bucket
to the prior edge, we determine the edge of the current bucket. (Note that for the
first bucket, we don't have a prior bucket to use. The edge is the same as the size.)
Later, when we need to know the last bucket edge—that is, the total width of the
group—we can retrieve the edge value of the last bucket. As a last step, we assign
the value of x to Result and then push the bucket onto the end of our vector.
After running InitVector() , our data will look like this ( i is the index of the
vector):
i
Size
Edge
Result
0
12
12
100
1
11
23
101
2
10
33
102
3
9
42
103
4
8
50
104
5
7
57
105
6
6
63
106
7
5
68
107
8
4
72
108
9
3
75
109
As we can see from the above table and from Figure 12.4, the edges represent
the cumulative sizes of the buckets as we “lay them end to end.� The total width of
all 10 buckets is 75.
S ELECTING A R ESULT
We can retrieve a random result out of the distribution using the binary search
method outlined above. The function GetResult() is entirely self-sufficient. That
is, we don't need to pass it or otherwise store the number of items in our distribu-
tion or the value of the right-most edge. When we call the function, it returns a
random result from mvDistribution determined by where the generated random
number lands in the distribution.
Search WWH ::




Custom Search