/[projectaxis]/projectaxis/projectAxis/src/map/extra_math.cpp
ViewVC logotype

Diff of /projectaxis/projectAxis/src/map/extra_math.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.1 by leiavoia, Fri Jun 6 23:27:44 2003 UTC revision 1.2 by leiavoia, Fri Aug 8 02:06:32 2003 UTC
# Line 1  Line 1 
1  # include <math.h>  #include <math.h>
2    #include <stdlib.h>
3    #include <iostream>
4    
 // extra_math.cc for blotchmaker  
5    
6    
7    
# Line 38  for (counter = 0; counter < num_list_ite Line 39  for (counter = 0; counter < num_list_ite
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    

Legend:
Removed from v.1.1  
changed lines
  Added in v.1.2

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26