1 |
# include <math.h> |
#include <math.h> |
2 |
|
#include <stdlib.h> |
3 |
|
#include <iostream> |
4 |
|
|
|
// extra_math.cc for blotchmaker |
|
5 |
|
|
6 |
|
|
7 |
|
|
39 |
|
|
40 |
|
|
41 |
|
|
42 |
|
// given a list of lotto balls and the total number of items in that list, picks an index |
43 |
|
// returns 0 otherwise (since there is usually a first index) |
44 |
|
int Lotto(int list[], int num_items) { |
45 |
|
|
46 |
|
// count them all for a total |
47 |
|
int total = 0; |
48 |
|
for(int x=0; x < num_items; x++) { |
49 |
|
total += list[x]; |
50 |
|
} |
51 |
|
|
52 |
|
// short circuit for floating point exceptions |
53 |
|
if (total == 0) {return 0;} |
54 |
|
|
55 |
|
// choose one |
56 |
|
int pick = int( rand() % (total) ); |
57 |
|
|
58 |
|
// find the index it belongs to |
59 |
|
int run = 0; |
60 |
|
for(int x=0; x < num_items; x++) { |
61 |
|
int was = run; |
62 |
|
run += list[x]; |
63 |
|
if ( (pick >= was) && (pick < run) ) {return x;} else {continue;} |
64 |
|
} |
65 |
|
|
66 |
|
return 0; |
67 |
|
} |
68 |
|
|
69 |
|
|
70 |
|
|
71 |
|
|
72 |
|
|