Simulation problem

Pages: 12
I am making a simulation program based on some old forum topic that I thought would be interesting to try and make it 100 shooters instead of 3 and to have the first shooter starting at a 1% kill chance and increment up by 1% so that the last shooter has a 100% kill chance but I clearly did something wrong but I am not sure exactly what I'm guessing something with my math lol here is the code/output:
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
#include <iostream>
#include <vector>
#include <stdlib.h>
#include <time.h>

const unsigned short alive( std::vector<bool> status , std::vector<bool>::iterator it )
{
    unsigned short amountAlive( 0 );
    for( it = status.begin(); it != status.end(); it++ )
    {
        if( *it )
        {
            amountAlive++;
        }
    }
    return( amountAlive );
}

const unsigned short firstAlive( std::vector<bool> status , std::vector<bool>::iterator it )
{
    it = status.begin();
    unsigned short position = 0;
    while( !*it )
    {
        it++;
        position++;
    }
    return( position );
}

const unsigned short lastAlive( std::vector<bool> status , std::vector<bool>::iterator it )
{
    unsigned int position = 0 , i = 0;
    for( it = status.begin(); it != status.end(); it++ )
    {
        if( *it )
        {
           position = i;
        }
        i++;
    }

    return( position );
}

const unsigned short GCF( const unsigned short low , const unsigned short high )
{
    unsigned int gcf = 1 , factorsOf100[ 9 ] = { 1 , 2 , 4 , 5 , 10 , 20 , 25 , 50 , 100 };
    for( unsigned int i = 0; i < 9; i++ )
    {
        if( factorsOf100[ i ] > low )
        {
            break;
        } else if( low % factorsOf100[ i ] == 0 && high % factorsOf100[ i ] == 0 )
        {
            gcf = factorsOf100[ i ];
        }
    }
    return( gcf );
}

const unsigned int winner( std::vector<bool> status , std::vector<bool>::iterator it )
{
    unsigned short position = 0;
    for( it = status.begin(); it != status.end(); it++ )
    {
        if( *it )
        {
            break;
        }
        position++;
    }
    return( position );
}
int main ( int argc , char** argv )
{
    srand( ( unsigned ) time( NULL ) );
    unsigned short wins[ 100 ] = { 0 };
    for( unsigned int i = 0; i < 10000; i++ ){
        std::vector<bool> status( 100 , true );
        std::vector<bool>::iterator it;
        unsigned short amountAlive( 100 ) , last = 99 , first = 0 , gcf = 1 , position = 0 , high = 100 , low = 1 , shooter = 0;
        while( amountAlive > 1 )
        {
            amountAlive = alive( status , it ); //get amount that is alive
            gcf = GCF( position + 1 , 100 ); //greatest common factor of current shooter and the total shooters
            high = 100 / gcf; //total shooters divided by gcf
            low = (position + 1) / gcf; //currenter shooter + 1 divided by gcf ( + 1 because you don't count ppl starting at 0 you count from 1 up. )

            if( rand() % ( high - low + 1 ) + low  == low ) //if the random number is the first possible number
            {
                if( position != last ) //if the current shooter is not the last shooter
                {
                    status[ last ] = false; //then shoot the last alive shooter
                } else {
                    status[ first ] = false; // otherwise shoot the first alive shooter
                }
            }

            last = lastAlive( status , it ); //get the position of the last alive
            first = firstAlive( status , it ); //get position of the first alive

            if( position == last ) //if it is the last shooter
            {
                position = first;
            } else {
                position++;
            }
        }
        shooter = winner( status , it );
        wins[ shooter ]++;
    }

    for( unsigned int i = 0; i < 100; i++ )
    {
        if( wins[ i ] > 0 )
        {
            std::cout << "Shooter " << i + 1 << " won " << wins[ i ] << " times!" << std::endl;
        }
    }

    return( 0 );
}
Shooter 1 won 2 times!
Shooter 2 won 17 times!
Shooter 3 won 24 times!
Shooter 4 won 166 times!
Shooter 5 won 250 times!
Shooter 6 won 112 times!
Shooter 7 won 61 times!
Shooter 8 won 189 times!
Shooter 9 won 29 times!
Shooter 10 won 104 times!
Shooter 11 won 4 times!
Shooter 12 won 3 times!
Shooter 13 won 1 times!
Shooter 14 won 1 times!

Process returned 0 (0x0)   execution time : 15.663 s
Press any key to continue.

As you can see clearly it shouldn't be just the first couple of shooters or maybe if I ran it like 1million or billion times it would give the higher up shooters atleast 1 win because I'm sure their win chances are EXTREMELY low but tbh I am too lazy to try and figure out their actual win rate with math which I probably should... and anyways that wouldn't solve my problem because if you add those up it isn't even 1000 like it is supposed to be when I added I got 963 so some how someone did not win 37 times =p

Thanks for any help suggestions ( you can take your time to respond I guess because I am just doing this for fun. Thought it would be something interesting to do )

[EDIT]
I made a formula to tell me the chance of Shooter 100 surviving..odds do not look like they are in his favor if my math is correct:
1
2
3
4
5
6
7
8
    double percent = 1 , chance = 100;
    for( unsigned int i = 1; i < 100; i++ )
    {
        percent = ( 100 - i );
        percent /= 100;
        chance *= percent;
    }
    std::cout << std::setprecision( 100 ) << chance << std::endl;
9.332621544394418831674905357515529698467311115540178051821062376319944130792489
5474888842766263694e-041

Process returned 0 (0x0)   execution time : 0.026 s
Press any key to continue.

tried setting precision to 1 billion but looks the same to me but anyways that mean he has a
.00000000000000000000000000000000000000000009333
chance to win LOL
[/EDIT]
Last edited on
Here is the new code but still not sure if it is accurate can someone overlook my code and let me know please doing the math formulas by hand would take extremely long but the 100th guy has about a .00000000000000000000000000000000000000000000933 chance to survive the FIRST round.
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>
#include <fstream>
/* Description:
there are 100 people each have a gun
They shoot in order from least accurate to most accurate
They shoot at the person who is the most accurate
who will survive?*/

class Shooter
{
private:
    std::vector<bool> status; //status if each shooter is alive or not there are 100 shooters
    std::vector<bool>::iterator it; //iterator for the shooters
    unsigned short amountAlive , shooter , target , first , gcf , i , high , low;
/* how many are alive , who is the current shooter, who is the current 
target , who is the first alive shooter, what is the greatest common factor 
between the current shooter and the total( 100 ), just a variable used to 
keep track of things, highest value for random( after the gcf kicks into play)
lowest value for random( after the gcf kicks into play)*/

public:
    unsigned short winner; //who the winner is
    Shooter( void )
    {
        status.assign( 100 , true ); //makes 100 shooters that are all alive
        it = status.begin(); //sets the iterator to the start of the shooter 
        //vector
        amountAlive = 100; /*sets the starting alive amount to 100 (there are
 100 shooters*/
        shooter = 0; /*sets current shooter to index 0 in the array ( first 
shooter )*/
        target = 99; //sets the target shooter to index 99 ( last shooter )
        first = 0; //first person is at index 0
        gcf = 1; //Greatest common factor of 1 and 100 is 1.
        i = 0; //sets default value for i to 0
        winner = 0; /*default value of winner is 0 ( there is no winner right 
now)*/
        high = 100; //highest value is 100 since there are 100 shooters
        low = 1; //lowest value is 1 since it starts at the first shooter

        while( amountAlive > 1 ) /*while there is atleast 2 people alive they will shoot at each other*/
        {
            shoot(); //begin shooting
            firstAlive(); //set the first shooter to the first alive shooter
            if( shooter == target ) /*if the shooter is the last alive shooter
make the next shooter the first otherwise make it the next shooter*/
            {
                shooter = first;
            } else {
                shooter++;
            }
            alive(); //get the new amount of alive people
            GCF(); //get the greatest common factor
            high = 100 / gcf; //set the highest value to the total( 100 ) and 
divide by the gcf*/
            low = ( shooter + 1 )/ gcf; //( get the current shooter and divide by gcf*/
/*The above two high and low are so shooter 1 has a 1% chance to hit and
each shooter goes up by 1% chance to hit so the 100th guy has a 100% chance to hit*/
            i = 0; //set i back to 0
            //std::cout << "shooter: " << shooter << std::endl;
            //std::cout << "target: " << target << std::endl;
            //std::cout << "Amount alive: " << amountAlive << std::endl;
            //std::cout << "First alive: " << first << std::endl;
/* these were before I fixed a few bugs wanted to check some info can be 
removed i suppose*/
        }
        winner = first; /*the winner is set to the first alive person( who also 
happens to be the only alive person left =p */
    }

    void alive( void ) //set the amount of alive people
    {
        amountAlive = 0; //set it equal to 0 so we can start the count over
        for( it = status.begin(); it != status.end(); it++ ) 
        {
            if( *it ) //if the person is alive we add one to the alive people
            {
                amountAlive++;
            }
        }
    }

    void firstAlive( void ) //we want to get the first alive shooter's index
    {
        it = status.begin(); //set the first iterator to the beginning
        first = 0; //set the first alive shooter to 0
        while( !*it ) /*while the current shooter is dead we add one to the 
iterator and to the first shooter position*/
        {
            it++;
            first++;
        }
    }

    void GCF( void ) //getting the Greatest Common Factor 
    {
        gcf = 1; /*set the greatest common factor to 1 since that is the lowest value that goes into 1 - > 100 and 100*/
        unsigned short factorsOf100[ 9 ] = { 2 , 4 , 5 , 10 , 20 , 25 , 50 , 100 };
//these are the only viable options for GCF for 1-> 100 and 100
        for( unsigned short i = 0; i < 9; i++ ) /*there are 9 elements in the array of choice*/
        {
            if( factorsOf100[ i ] > shooter ) /*if the gcf is larger than the shooter
we stop the loop otherwise we see if the remainder of the two is 0*/
            {
                break;
            } else if( shooter % factorsOf100[ i ] == 0 && 100 % factorsOf100[ i ] == 0 )
            {
                gcf = factorsOf100[ i ];  /*set the gcf to the greatest common factor found*/
            }
        }
    }

    void shoot( void ) //shoot the most accurate person
    {
        if( rand() % ( high - low + 1 ) + low == low ) /*check if the current
shooter hits his target or not if so and the shooter is aiming at himself
we shoot the first alive person otherwise we shoot the most accurate person*/
        {
            if( shooter == target )
            {
                status[ first ] = false;
            } else {
                status[ target ] = false;
                target--;
            }
        }
    }
};

int main()
{
    srand( ( unsigned ) time( NULL ) ); //get a random seed
    std::vector<unsigned short> wins( 100 , 0 ); /*vector that contains the wins and sets them by default to 0*/
    std::vector<unsigned short>::iterator it; //iterator for the win vector
    std::ofstream out( "data.txt" ); //output to a file called data.txt
    unsigned short i = 0; /* new value set to 0 so we can keep track of the shooters*/

    Shooter *ptr = NULL; /*create a pointer to a Shooter object and set to 
null*/
    for( unsigned short i = 0; i < 10000; i++ ) //run the simulation 10k times
    {
        ptr = new Shooter; //create a new shooter ( which runs my test )
        wins[ ptr->winner ]++; //add one to the shooters win
        delete ptr; //delete the shooter object
    }
    for( it = wins.begin(); it != wins.end(); it++ ) //output the wins and ratio
    {
        out << "Shooter " << i + 1 << " has " << *it<< " win(s). With a winrate of " << ( double ) *it / 10000 << std::endl;
        i++;
    }

    return 0;
}

Thanks for overlooking it =]
Last edited on
"overlook" --> "look over"

Those crazy numbers are usually from uninitialized variables. Your shooter class has a lot of 'em. I suggest using an initializer list in your constructor to keep track of your variables a little easier.
For example:
1
2
3
4
5
Shooter::Shooter():
   amountAlive(100), shooter(0), target(99), 
   first(0), gcf(1), I(0),  //Capital I because of my incorrigible auto-correct
   win(0), high(100), low(1)
{}

However, the sheer number of private variables for just a shooter object seems suspicious...

Now that I helped you, do you mind helping me with my Linked List question?

Edit:
Almost forgot. stdlib.h and time.h are C headers. For C++, use the cstdlib and ctime headers.
Last edited on
Well after I rewrote it, it has the correct amount of results and that crazy number is just the chance if you do the math =p so I think my computer would have to be on for like a year straight compiling to get 1 win for the 100th guy unless I just get extremely lucky and some how every1 keeps missing him for 99 rounds straight =p Other than that I was just wondering if I was wondering if my formulas sounded correct =p my current results are
Oh and I added a winrate to the program editing old code now.
Shooter 1 has 13 win(s). With a winrate of 0.0013
Shooter 2 has 84 win(s). With a winrate of 0.0084
Shooter 3 has 466 win(s). With a winrate of 0.0466
Shooter 4 has 516 win(s). With a winrate of 0.0516
Shooter 5 has 2604 win(s). With a winrate of 0.2604
Shooter 6 has 2583 win(s). With a winrate of 0.2583
Shooter 7 has 961 win(s). With a winrate of 0.0961
Shooter 8 has 531 win(s). With a winrate of 0.0531
Shooter 9 has 1375 win(s). With a winrate of 0.1375
Shooter 10 has 152 win(s). With a winrate of 0.0152
Shooter 11 has 670 win(s). With a winrate of 0.067
Shooter 12 has 11 win(s). With a winrate of 0.0011
Shooter 13 has 21 win(s). With a winrate of 0.0021
Shooter 14 has 4 win(s). With a winrate of 0.0004
Shooter 15 has 4 win(s). With a winrate of 0.0004
Shooter 16 has 4 win(s). With a winrate of 0.0004
Shooter 17 has 1 win(s). With a winrate of 0.0001
Shooter 18 has 0 win(s). With a winrate of 0
Shooter 19 has 0 win(s). With a winrate of 0
Shooter 20 has 0 win(s). With a winrate of 0
Shooter 21 has 0 win(s). With a winrate of 0
Shooter 22 has 0 win(s). With a winrate of 0
Shooter 23 has 0 win(s). With a winrate of 0
Shooter 24 has 0 win(s). With a winrate of 0
Shooter 25 has 0 win(s). With a winrate of 0
Shooter 26 has 0 win(s). With a winrate of 0
Shooter 27 has 0 win(s). With a winrate of 0
Shooter 28 has 0 win(s). With a winrate of 0
Shooter 29 has 0 win(s). With a winrate of 0
Shooter 30 has 0 win(s). With a winrate of 0
Shooter 31 has 0 win(s). With a winrate of 0
Shooter 32 has 0 win(s). With a winrate of 0
Shooter 33 has 0 win(s). With a winrate of 0
Shooter 34 has 0 win(s). With a winrate of 0
Shooter 35 has 0 win(s). With a winrate of 0
Shooter 36 has 0 win(s). With a winrate of 0
Shooter 37 has 0 win(s). With a winrate of 0
Shooter 38 has 0 win(s). With a winrate of 0
Shooter 39 has 0 win(s). With a winrate of 0
Shooter 40 has 0 win(s). With a winrate of 0
Shooter 41 has 0 win(s). With a winrate of 0
Shooter 42 has 0 win(s). With a winrate of 0
Shooter 43 has 0 win(s). With a winrate of 0
Shooter 44 has 0 win(s). With a winrate of 0
Shooter 45 has 0 win(s). With a winrate of 0
Shooter 46 has 0 win(s). With a winrate of 0
Shooter 47 has 0 win(s). With a winrate of 0
Shooter 48 has 0 win(s). With a winrate of 0
Shooter 49 has 0 win(s). With a winrate of 0
Shooter 50 has 0 win(s). With a winrate of 0
Shooter 51 has 0 win(s). With a winrate of 0
Shooter 52 has 0 win(s). With a winrate of 0
Shooter 53 has 0 win(s). With a winrate of 0
Shooter 54 has 0 win(s). With a winrate of 0
Shooter 55 has 0 win(s). With a winrate of 0
Shooter 56 has 0 win(s). With a winrate of 0
Shooter 57 has 0 win(s). With a winrate of 0
Shooter 58 has 0 win(s). With a winrate of 0
Shooter 59 has 0 win(s). With a winrate of 0
Shooter 60 has 0 win(s). With a winrate of 0
Shooter 61 has 0 win(s). With a winrate of 0
Shooter 62 has 0 win(s). With a winrate of 0
Shooter 63 has 0 win(s). With a winrate of 0
Shooter 64 has 0 win(s). With a winrate of 0
Shooter 65 has 0 win(s). With a winrate of 0
Shooter 66 has 0 win(s). With a winrate of 0
Shooter 67 has 0 win(s). With a winrate of 0
Shooter 68 has 0 win(s). With a winrate of 0
Shooter 69 has 0 win(s). With a winrate of 0
Shooter 70 has 0 win(s). With a winrate of 0
Shooter 71 has 0 win(s). With a winrate of 0
Shooter 72 has 0 win(s). With a winrate of 0
Shooter 73 has 0 win(s). With a winrate of 0
Shooter 74 has 0 win(s). With a winrate of 0
Shooter 75 has 0 win(s). With a winrate of 0
Shooter 76 has 0 win(s). With a winrate of 0
Shooter 77 has 0 win(s). With a winrate of 0
Shooter 78 has 0 win(s). With a winrate of 0
Shooter 79 has 0 win(s). With a winrate of 0
Shooter 80 has 0 win(s). With a winrate of 0
Shooter 81 has 0 win(s). With a winrate of 0
Shooter 82 has 0 win(s). With a winrate of 0
Shooter 83 has 0 win(s). With a winrate of 0
Shooter 84 has 0 win(s). With a winrate of 0
Shooter 85 has 0 win(s). With a winrate of 0
Shooter 86 has 0 win(s). With a winrate of 0
Shooter 87 has 0 win(s). With a winrate of 0
Shooter 88 has 0 win(s). With a winrate of 0
Shooter 89 has 0 win(s). With a winrate of 0
Shooter 90 has 0 win(s). With a winrate of 0
Shooter 91 has 0 win(s). With a winrate of 0
Shooter 92 has 0 win(s). With a winrate of 0
Shooter 93 has 0 win(s). With a winrate of 0
Shooter 94 has 0 win(s). With a winrate of 0
Shooter 95 has 0 win(s). With a winrate of 0
Shooter 96 has 0 win(s). With a winrate of 0
Shooter 97 has 0 win(s). With a winrate of 0
Shooter 98 has 0 win(s). With a winrate of 0
Shooter 99 has 0 win(s). With a winrate of 0
Shooter 100 has 0 win(s). With a winrate of 0


Also Daleth what uninitialized variable do I have? and I did what you did already =p
Last edited on
Oh, when I said "...and you have a lot of 'em" I meant a lot of variables, not uninitialized variable. Whoops.

The number of wins the few dozen shooters have, though, is pretty staggering... Do you mind leaving some comments in your code to explain what each variable is for and what each function does? I'm not so good when it comes to reading a lot of code.
The results sounds about right to me but I am not exactly positive if they are correct and yeah I'll comment it now.
Oh, wait. Do the shooters have health points? I was assuming one shot one kill.
Also, shouldn't the wins be more spread out among the shooters and not just with the first dozen?
No because they are constantly shooting at the person with the highest percent and nah they have 1 life =p if they get shot they are dead but each person goes up by 1% hit chance starting at 1% working up to 100% I just commented it out now sorry it took so long ps I am running the simulation 1Million times now I'll let you know the results when it finishes I'm guessing not for a while( changed the shorts in the main function to long for the 1Million test)
Last edited on
I haven't gone through the entire program yet, but I see some design issues.

1) You are putting all the action of shooting and finding the winner within the Shooter constructor. Shouldn't you just create some number of Shooter's inside main and from there start the shooting?
2) You use one pointer to create a set of 100 shooters some number of times, continuously creating new objects and deleting them afterward. Why not just use while loops and have multiple Shooter objects?
(Also this explains the win numbers, so now I understand)

Is the percentage calculation still a problem? I'm still scouring the code for any specific issues.
I did create multiple shooter objects but I deleted them when I was done with them or are you suggesting I use a vector of the objects? like
1
2
3
4
5
6
std::vector<Shooter> shooter( SIZE );
std::vectro<Shooter>::iterator it;
for( it = shooter.begin(); it != shooter.end(); it++ )
{
     wins[ shooter[ i ].winner ]++;
}


Also I suppose I could of left the shoot function outside of the constructor but then after creating the object I would just have to call that function anyways so I figured it would be more handy just to do it all at one time
Thanks again for your time and my 1Million Simulations is still running lol...it on average takes about 2000 shots to kill everyone except one person ( maybe more I am just guessing I never actually used a variable to keep track of that) and it's doing that 1 million times.
Last edited on
Okay the program finally finished loading after running 1Million times here is my results:
Shooter 1 has 1165 win(s). With a winrate of 0.001165
Shooter 2 has 8024 win(s). With a winrate of 0.008024
Shooter 3 has 48867 win(s). With a winrate of 0.048867
Shooter 4 has 53550 win(s). With a winrate of 0.05355
Shooter 5 has 258822 win(s). With a winrate of 0.258822
Shooter 6 has 264117 win(s). With a winrate of 0.264117
Shooter 7 has 96750 win(s). With a winrate of 0.09675
Shooter 8 has 51665 win(s). With a winrate of 0.051665
Shooter 9 has 131714 win(s). With a winrate of 0.131714
Shooter 10 has 15587 win(s). With a winrate of 0.015587
Shooter 11 has 65610 win(s). With a winrate of 0.06561
Shooter 12 has 983 win(s). With a winrate of 0.000983
Shooter 13 has 2287 win(s). With a winrate of 0.002287
Shooter 14 has 269 win(s). With a winrate of 0.000269
Shooter 15 has 301 win(s). With a winrate of 0.000301
Shooter 16 has 247 win(s). With a winrate of 0.000247
Shooter 17 has 30 win(s). With a winrate of 3e-005
Shooter 18 has 1 win(s). With a winrate of 1e-006
Shooter 19 has 7 win(s). With a winrate of 7e-006
Shooter 20 has 0 win(s). With a winrate of 0
Shooter 21 has 4 win(s). With a winrate of 4e-006
Shooter 22 has 0 win(s). With a winrate of 0
Shooter 23 has 0 win(s). With a winrate of 0
Shooter 24 has 0 win(s). With a winrate of 0
Shooter 25 has 0 win(s). With a winrate of 0
Shooter 26 has 0 win(s). With a winrate of 0
Shooter 27 has 0 win(s). With a winrate of 0
Shooter 28 has 0 win(s). With a winrate of 0
Shooter 29 has 0 win(s). With a winrate of 0
Shooter 30 has 0 win(s). With a winrate of 0
Shooter 31 has 0 win(s). With a winrate of 0
Shooter 32 has 0 win(s). With a winrate of 0
Shooter 33 has 0 win(s). With a winrate of 0
Shooter 34 has 0 win(s). With a winrate of 0
Shooter 35 has 0 win(s). With a winrate of 0
Shooter 36 has 0 win(s). With a winrate of 0
Shooter 37 has 0 win(s). With a winrate of 0
Shooter 38 has 0 win(s). With a winrate of 0
Shooter 39 has 0 win(s). With a winrate of 0
Shooter 40 has 0 win(s). With a winrate of 0
Shooter 41 has 0 win(s). With a winrate of 0
Shooter 42 has 0 win(s). With a winrate of 0
Shooter 43 has 0 win(s). With a winrate of 0
Shooter 44 has 0 win(s). With a winrate of 0
Shooter 45 has 0 win(s). With a winrate of 0
Shooter 46 has 0 win(s). With a winrate of 0
Shooter 47 has 0 win(s). With a winrate of 0
Shooter 48 has 0 win(s). With a winrate of 0
Shooter 49 has 0 win(s). With a winrate of 0
Shooter 50 has 0 win(s). With a winrate of 0
Shooter 51 has 0 win(s). With a winrate of 0
Shooter 52 has 0 win(s). With a winrate of 0
Shooter 53 has 0 win(s). With a winrate of 0
Shooter 54 has 0 win(s). With a winrate of 0
Shooter 55 has 0 win(s). With a winrate of 0
Shooter 56 has 0 win(s). With a winrate of 0
Shooter 57 has 0 win(s). With a winrate of 0
Shooter 58 has 0 win(s). With a winrate of 0
Shooter 59 has 0 win(s). With a winrate of 0
Shooter 60 has 0 win(s). With a winrate of 0
Shooter 61 has 0 win(s). With a winrate of 0
Shooter 62 has 0 win(s). With a winrate of 0
Shooter 63 has 0 win(s). With a winrate of 0
Shooter 64 has 0 win(s). With a winrate of 0
Shooter 65 has 0 win(s). With a winrate of 0
Shooter 66 has 0 win(s). With a winrate of 0
Shooter 67 has 0 win(s). With a winrate of 0
Shooter 68 has 0 win(s). With a winrate of 0
Shooter 69 has 0 win(s). With a winrate of 0
Shooter 70 has 0 win(s). With a winrate of 0
Shooter 71 has 0 win(s). With a winrate of 0
Shooter 72 has 0 win(s). With a winrate of 0
Shooter 73 has 0 win(s). With a winrate of 0
Shooter 74 has 0 win(s). With a winrate of 0
Shooter 75 has 0 win(s). With a winrate of 0
Shooter 76 has 0 win(s). With a winrate of 0
Shooter 77 has 0 win(s). With a winrate of 0
Shooter 78 has 0 win(s). With a winrate of 0
Shooter 79 has 0 win(s). With a winrate of 0
Shooter 80 has 0 win(s). With a winrate of 0
Shooter 81 has 0 win(s). With a winrate of 0
Shooter 82 has 0 win(s). With a winrate of 0
Shooter 83 has 0 win(s). With a winrate of 0
Shooter 84 has 0 win(s). With a winrate of 0
Shooter 85 has 0 win(s). With a winrate of 0
Shooter 86 has 0 win(s). With a winrate of 0
Shooter 87 has 0 win(s). With a winrate of 0
Shooter 88 has 0 win(s). With a winrate of 0
Shooter 89 has 0 win(s). With a winrate of 0
Shooter 90 has 0 win(s). With a winrate of 0
Shooter 91 has 0 win(s). With a winrate of 0
Shooter 92 has 0 win(s). With a winrate of 0
Shooter 93 has 0 win(s). With a winrate of 0
Shooter 94 has 0 win(s). With a winrate of 0
Shooter 95 has 0 win(s). With a winrate of 0
Shooter 96 has 0 win(s). With a winrate of 0
Shooter 97 has 0 win(s). With a winrate of 0
Shooter 98 has 0 win(s). With a winrate of 0
Shooter 99 has 0 win(s). With a winrate of 0
Shooter 100 has 0 win(s). With a winrate of 0

Jeez those statistics if I made my program correctly do not look too good for for 98% of the shooters lol. And even then I wouldn't want a 26% survival chance at a maximum does my formula for checking the survivor seem correct to everyone?

PS:
it took 6096.651 Seconds to finish so um that is.... 101.611 minutes...or a little over an hour and a half anyone have any suggestions I could use to improve the speed to run my program?
Last edited on
Well, yeah as far as organization goes. I mean... here's how I would design your program. (Note that it does not do everything that you wanted and there are a couple bugs that I am too lazy to fix.) You can compare its speed to your program.

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
#include <iostream>
#include <string>
#include <ctime>
#include <cstdlib>
#include <limits>

using std::cout;
using std::cin;
using std::string;
using std::endl;

struct Shooter{
	bool alive;
	int accuracy,	//use int because i'm lazy 
		kills;
	Shooter();
	~Shooter();
};
Shooter::Shooter():
	alive(true),
	accuracy(0),
	kills(0)
{} //Handle accuracy setting in main
Shooter::~Shooter(){}
	//Duel between two shooters
bool duel(Shooter& first, Shooter& target){
	int	
			//Magic number that determines if target is dead
		chance = rand()%100 + 1,
			//Create arbitrary range based on accuracy
		range_max = rand()%100 + 1 + first.accuracy/2,
		range_min = rand()%100 + 1 - first.accuracy/2
	;
		//Check if the range is beyond the 1-100 block
	if(range_max > 100){
		range_max = 100;
		range_min = 100 - first.accuracy;
	}else if(range_max < 0){
		range_max = first.accuracy;
		range_min = 0;
	}else if(range_min < 0){
		range_max = first.accuracy;
		range_min = 0;
	}else if(range_min > 100){
		range_max = 100;
		range_min = 100 - first.accuracy;
	}
		//if chance is within range, target is dead
	if(
		chance >= range_min && 
		chance <= range_max
	){
		target.alive = false;
		first.kills++;
		return true;
	}
		//shooter missed
	return false;
}
	//central function for getting user input to modify number variables
void PromptUserFor(int& tomodify, const string& name, int limit = std::numeric_limits<int>::max()){
	cout << "Please enter in the new " << name << ": ";
	while(!(cin >> tomodify) || tomodify <= 0 || tomodify > limit){
		cin.clear();
		cout << "Enter in a new value: ";
		cin >> tomodify;
	}
	cout << endl;
}

int main(int argc, char **argv){
	int initial_population, runs, current_population;
		//Have user set up initial values
	PromptUserFor(initial_population, "Initial Population");
		current_population = initial_population;
	PromptUserFor(runs, "Number of Runs");
		//Create number of shooters based on user input
	Shooter* person = new Shooter[initial_population];
		//Set up shooter accuracies
	for(int i = 0; i < initial_population; i++)
		PromptUserFor(person[i].accuracy, "Accuracy of Next Shooter", 100);
		//Start the shoot off
	cout << "Thank you for the information! Please wait while the shooters kill each other..." << endl;
		//Go through number of runs
	for(int run_ = 0; run_ < runs; run_++){
				//Pick shooter
		for(int currentshooter = 0; currentshooter < initial_population; currentshooter++){
			if(! person[currentshooter].alive )	continue;	//dead people not allowed to shoot
			//Pick target
			for(int currenttarget = 0; currenttarget < initial_population; currenttarget++){
				if(
					currentshooter == currenttarget ||
					! person[currenttarget].alive
				)	continue;	//avoid shooting himself or shooting corpses
					//The actual shot; no boom sound effects :(
					//Did shooter hit?
				cout << "PEW ";
				if( duel(person[currentshooter],person[currenttarget]) )
					current_population--;
			}
			if(current_population == 0)	break;
		}
			//reset alive bools
		for(int I = 0; I < initial_population; I++)
			person[I].alive = true;
	}
		//Display number of kills
	cout << endl << "\n\nAll finished killing shooters!" << endl;
	for(int i = 0; i < initial_population; i++){
		cout << "Shooter " << i+1 << " has " << person[i].kills << " kills"
			<< " with a win percentage of " << (double(person[i].kills)/(runs*initial_population))*100 << "%.\n";
	}
	cout << endl;
		//Clean up
	delete[] person;
	return 0;
}


Here's a sample output:

Please enter in the new Initial Population: 10

Please enter in the new Number of Runs: 50

Please enter in the new Accuracy of Next Shooter: 23

Please enter in the new Accuracy of Next Shooter: 74

Please enter in the new Accuracy of Next Shooter: 50

Please enter in the new Accuracy of Next Shooter: 1

Please enter in the new Accuracy of Next Shooter: 100

Please enter in the new Accuracy of Next Shooter: 34

Please enter in the new Accuracy of Next Shooter: 76

Please enter in the new Accuracy of Next Shooter: 87

Please enter in the new Accuracy of Next Shooter: 23

Please enter in the new Accuracy of Next Shooter: 65

Thank you for the information! Please wait while the shooters kill each other...

PEW PEW PEW PEW PEW PEW PEW PEW PEW PEW PEW PEW PEW PEW PEW PEW PEW PEW PEW PEW
(Whole lotta pew pews later...)
PEW PEW PEW PEW PEW PEW PEW PEW PEW PEW PEW PEW PEW PEW PEW PEW PEW PEW PEW PEW
PEW


All finished killing shooters!
Shooter 1 has 104 kills with a win percentage of 20.8%.
Shooter 2 has 145 kills with a win percentage of 29%.
Shooter 3 has 49 kills with a win percentage of 9.8%.
Shooter 4 has 20 kills with a win percentage of 4%.
Shooter 5 has 28 kills with a win percentage of 5.6%.
Shooter 6 has 4 kills with a win percentage of 0.8%.
Shooter 7 has 37 kills with a win percentage of 7.4%.
Shooter 8 has 11 kills with a win percentage of 2.2%.
Shooter 9 has 0 kills with a win percentage of 0%.
Shooter 10 has 3 kills with a win percentage of 0.6%.


Process returned 0 (0x0)   execution time : 20.564 s
Press any key to continue.
Shouldn't there be 450 dead ppl and not 401 Also I ran mine with 100 people and 1Million times
If I run my program 50 times like you ( with my 100 people) it takes me .557 seconds so wouldn't that mean mine is faster than yours? =p
Shooter 1 has 0 win(s). With a winrate of 0 %.
Shooter 2 has 0 win(s). With a winrate of 0 %.
Shooter 3 has 3 win(s). With a winrate of 6 %.
Shooter 4 has 3 win(s). With a winrate of 6 %.
Shooter 5 has 16 win(s). With a winrate of 32 %.
Shooter 6 has 9 win(s). With a winrate of 18 %.
Shooter 7 has 2 win(s). With a winrate of 4 %.
Shooter 8 has 8 win(s). With a winrate of 16 %.
Shooter 9 has 5 win(s). With a winrate of 10 %.
Shooter 10 has 3 win(s). With a winrate of 6 %.
Shooter 11 has 1 win(s). With a winrate of 2 %.
Shooter 12 has 0 win(s). With a winrate of 0 %.
Shooter 13 has 0 win(s). With a winrate of 0 %.
Shooter 14 has 0 win(s). With a winrate of 0 %.
Shooter 15 has 0 win(s). With a winrate of 0 %.
Shooter 16 has 0 win(s). With a winrate of 0 %.
Shooter 17 has 0 win(s). With a winrate of 0 %.
Shooter 18 has 0 win(s). With a winrate of 0 %.
Shooter 19 has 0 win(s). With a winrate of 0 %.
Shooter 20 has 0 win(s). With a winrate of 0 %.
Shooter 21 has 0 win(s). With a winrate of 0 %.
Shooter 22 has 0 win(s). With a winrate of 0 %.
Shooter 23 has 0 win(s). With a winrate of 0 %.
Shooter 24 has 0 win(s). With a winrate of 0 %.
Shooter 25 has 0 win(s). With a winrate of 0 %.
Shooter 26 has 0 win(s). With a winrate of 0 %.
Shooter 27 has 0 win(s). With a winrate of 0 %.
Shooter 28 has 0 win(s). With a winrate of 0 %.
Shooter 29 has 0 win(s). With a winrate of 0 %.
Shooter 30 has 0 win(s). With a winrate of 0 %.
Shooter 31 has 0 win(s). With a winrate of 0 %.
Shooter 32 has 0 win(s). With a winrate of 0 %.
Shooter 33 has 0 win(s). With a winrate of 0 %.
Shooter 34 has 0 win(s). With a winrate of 0 %.
Shooter 35 has 0 win(s). With a winrate of 0 %.
Shooter 36 has 0 win(s). With a winrate of 0 %.
Shooter 37 has 0 win(s). With a winrate of 0 %.
Shooter 38 has 0 win(s). With a winrate of 0 %.
Shooter 39 has 0 win(s). With a winrate of 0 %.
Shooter 40 has 0 win(s). With a winrate of 0 %.
Shooter 41 has 0 win(s). With a winrate of 0 %.
Shooter 42 has 0 win(s). With a winrate of 0 %.
Shooter 43 has 0 win(s). With a winrate of 0 %.
Shooter 44 has 0 win(s). With a winrate of 0 %.
Shooter 45 has 0 win(s). With a winrate of 0 %.
Shooter 46 has 0 win(s). With a winrate of 0 %.
Shooter 47 has 0 win(s). With a winrate of 0 %.
Shooter 48 has 0 win(s). With a winrate of 0 %.
Shooter 49 has 0 win(s). With a winrate of 0 %.
Shooter 50 has 0 win(s). With a winrate of 0 %.
Shooter 51 has 0 win(s). With a winrate of 0 %.
Shooter 52 has 0 win(s). With a winrate of 0 %.
Shooter 53 has 0 win(s). With a winrate of 0 %.
Shooter 54 has 0 win(s). With a winrate of 0 %.
Shooter 55 has 0 win(s). With a winrate of 0 %.
Shooter 56 has 0 win(s). With a winrate of 0 %.
Shooter 57 has 0 win(s). With a winrate of 0 %.
Shooter 58 has 0 win(s). With a winrate of 0 %.
Shooter 59 has 0 win(s). With a winrate of 0 %.
Shooter 60 has 0 win(s). With a winrate of 0 %.
Shooter 61 has 0 win(s). With a winrate of 0 %.
Shooter 62 has 0 win(s). With a winrate of 0 %.
Shooter 63 has 0 win(s). With a winrate of 0 %.
Shooter 64 has 0 win(s). With a winrate of 0 %.
Shooter 65 has 0 win(s). With a winrate of 0 %.
Shooter 66 has 0 win(s). With a winrate of 0 %.
Shooter 67 has 0 win(s). With a winrate of 0 %.
Shooter 68 has 0 win(s). With a winrate of 0 %.
Shooter 69 has 0 win(s). With a winrate of 0 %.
Shooter 70 has 0 win(s). With a winrate of 0 %.
Shooter 71 has 0 win(s). With a winrate of 0 %.
Shooter 72 has 0 win(s). With a winrate of 0 %.
Shooter 73 has 0 win(s). With a winrate of 0 %.
Shooter 74 has 0 win(s). With a winrate of 0 %.
Shooter 75 has 0 win(s). With a winrate of 0 %.
Shooter 76 has 0 win(s). With a winrate of 0 %.
Shooter 77 has 0 win(s). With a winrate of 0 %.
Shooter 78 has 0 win(s). With a winrate of 0 %.
Shooter 79 has 0 win(s). With a winrate of 0 %.
Shooter 80 has 0 win(s). With a winrate of 0 %.
Shooter 81 has 0 win(s). With a winrate of 0 %.
Shooter 82 has 0 win(s). With a winrate of 0 %.
Shooter 83 has 0 win(s). With a winrate of 0 %.
Shooter 84 has 0 win(s). With a winrate of 0 %.
Shooter 85 has 0 win(s). With a winrate of 0 %.
Shooter 86 has 0 win(s). With a winrate of 0 %.
Shooter 87 has 0 win(s). With a winrate of 0 %.
Shooter 88 has 0 win(s). With a winrate of 0 %.
Shooter 89 has 0 win(s). With a winrate of 0 %.
Shooter 90 has 0 win(s). With a winrate of 0 %.
Shooter 91 has 0 win(s). With a winrate of 0 %.
Shooter 92 has 0 win(s). With a winrate of 0 %.
Shooter 93 has 0 win(s). With a winrate of 0 %.
Shooter 94 has 0 win(s). With a winrate of 0 %.
Shooter 95 has 0 win(s). With a winrate of 0 %.
Shooter 96 has 0 win(s). With a winrate of 0 %.
Shooter 97 has 0 win(s). With a winrate of 0 %.
Shooter 98 has 0 win(s). With a winrate of 0 %.
Shooter 99 has 0 win(s). With a winrate of 0 %.
Shooter 100 has 0 win(s). With a winrate of 0 %.

Process returned 0 (0x0)   execution time : 0.557 s
Press any key to continue.


[edit]OH duh.. you probably had your input waiting for a while lol....[/edit]
Last edited on
Because one shooter is always alive in each run. :3
That extra one is odd though. Should be 400. Oh well...
no it should be 450 9 x 50 = 450 =p
Oh right. xD Well, I have no idea. :/ Perhaps one of the runs is skipped for some reason.
ah and maybe I'll try to re-code my program again to try and make it faster but I just wanted to know if my program was outputting correct info or if somewhere in my program I was missing a variable or something that made them shoot the wrong people or something
Now that would be VERY difficult to debug or spot. :(
Wanna start up your code on github?
what is github?
It's basically a cloud service for program files.
Pages: 12