I'm working on a battle algorithm for a text-based game and I stumble when it comes to randomly distributing attacks on a set of units.
E.G, 100 archers shoot 100 targets at the same time. I thought of doing like this :
- The 100 targets are considered as a single block
- The arrows are distributed inside
- The units hit the same number of times are grouped together.
- I manage the life of each new group
- Repeat on each new group if necessary
I can distribute the arrows (step 2) and get the result (step 3) through a loop, but with many armies composed of millions archers, computing time & memory consumption becomes a problem.
// Step 2
const targets = new Array( 1000000000 );
targets.fill( 0, 0, 1000000000 );
const arrows = 1000000000;
for( let i = 0; i < arrows; i++ ){
const index = Math.floor( ( Math.random() * arrows ) );
targets[ index ] += 1
}
//Step 3
const result = []
targets.forEach( value => {
if( value ){
if( result[ value ] ) result[ value ] += 1
else result[ value ] = 1
}
} )
Is there another way to solve this problem ? I believe that with large numbers, directly calculating the most likely scenario may be a good option. Unfortunately, I do not know anything about probabilities.