# How do I apply different probability factors in an algorithm for a cricket simulation game?

I am trying to write the algorithm for a cricket simulation game which generates runs on each ball between 0 to 6. The run rate or runs generated changes when these factors come into play like skill of the batsman, skill of the bowler, target to be chased. Wickets left.

If the batsman is skilled more runs will be generated. There will be a mode of play of the batsman aggressive, normal, defensive. If he plays aggressive chances of getting out will be more. If the chasing target is more the run rate should be more. If the overs are final the run rate should be more.

I am using java random number function for this. The code so far I've written is

    public class Cricket {

public static void main(String args[])
{
int totalRuns=0;
//i is the balls bowled
for (int i = 1; i <= 60 ; i++)
{
int RunsPerBall = (int)(Math.random()*6);
//System.out.println(Random);
totalRuns=totalRuns+RunsPerBall;
}
System.out.println(totalRuns);
}

}


Can somebody help me how to apply the factors in the code. I believe probability will be used with this. I am not clear how to apply the probability of the factors stated above in the code.

• I suggest you start writing an equation on paper with these factors. Basic algebra should be enough to think about if a factor will add/subtract/multiply/divide the number of runs. – ashes999 Jul 21 '13 at 21:19

You can work your way backwards by creating a series of grids and calculating the stats for each grid.

Imagine for example 16 balls being bowled at a player (4 down the leg side, 4 down the off side and eight on target and the expected outcome for each one (ignoring dot balls where no runs are scored or wickets taken):

a) Batsman vs Poor Bowler

6 1 1 1    <-- Full Toss
1 2 1 1    <-- Overpitched
1 W 1 1    <-- Good Length
1 1 1 4    <-- Short of a length


total runs = 24 outs = 1 bowler's strike rate is 16 - batsman's average = 24 Run Rate=1.5

b) Batsman vs Medium Bowler

6 1 1 1
1 2 W 1
1 W 2 1
1 1 1 4


total runs 24 outs= 2 bowler's strike rate is 8 - batsman's average = 12 Run Rate=1.5

c) Batsman vs Good Bowler

W 1 2 1
1 2 W 1
1 W W 1
6 2 2 4


total runs = 24 outs = 4 bowler's strike rate 4 batsman's average = 6 Run Rate=1.5

So for a NxN grid

• The bowler's strike rate is N * N/Number of Wickets in grid (excluding stumpings)
• The batting average is Number of Runs in grid/ Number of Wickets in grid
• The run rate is Number of runs in grid/ N * N

Now if all batsmen were of equal quality, you could choose between grid a) grid b) and grid c) based on the strike rate of the bowler, if the bowler has an average of 16 then always use grid a), if the bowler has an average 12 then half the time chose a) and half the time b) etc.

You can change the quality of the batsman in the same way, e.g. by reducing the number of runs within the grid:

d) Poor Batsman vs Poor Bowler

1 1 1 1
1 2 1 1
1 W 1 1
1 1 1 1


total runs = 16 outs= 1 bowler's strike rate 16 batsman's average = 16 Run Rate=1

e) Poor Batsman vs Medium Bowler

1 1 1 1
1 2 W 1
1 W 1 1
1 1 1 2


total runs = 16 outs = 2 bowler's strike rate 8 batsman's average = 8 Run Rate=1

f) Poor Batsman vs Good Bowler

2 1 1 1
1 W 1 1
1 W W 1
1 1 W 4


total runs = 16 outs= 4 bowler's strike rate 4 batsman's average = 4 Run Rate = 1

You could also increase aggression i.e. increase the run rate & strike rate simultaneously by reducing the number of dot balls per over.

Note: Never played cricket.

I would use multipliers. Multiply your randomly generated number by values based on player skill and mode of play. These numbers will be close to 1 (~.5 - 2). Compiling several multipliers may be tricky (5 * .8 * 1.2 != 5).

Going out may been a true/false state with a threshold. Random 0-1 and if over X then they go out. Repeat for each way to go out. Set the threshold based on skill and mode of play. Or each way to go out has a baseline threshold which is multiplied by another set of multipliers.

Deciding these multipliers may be difficult. If you are using real people, you could find a way to convert their season stats to these multipliers.

Also don't use (int) double for converting from a decimal value to an integer. I truncates the value(4.2 and 4.8 both round to 4). They are multiple ways to round in Java; the most common is Math.round().

One option is to calculate a positive number based on the chance of each event happening, for each ball bowled. Here's some examples, with some randomness and weightings thrown in. You can get away without randomness in this part if you want to:

Chance of being out = max(0, (rand() % BatsmanSkill) - (rand() % (BowlerSkill + BatsmanAggression)))
Chance of scoring 6 = max(0, (rand() % (BatsmanSkill + (BatsmanAggression * 2))) - (rand() % (BowlerSkill)))
Chance of scoring 4 = max(0, (rand() % (BatsmanSkill + (BatsmanAggression))) - (rand() % (BowlerSkill)))
// TODO: Fill in 5, and 0-3, and other influences on the outcome


Once you have all the numbers, you can then randomly pick between them based on their weights:

Outcome = rand() % sum_of_chances;
if (Outcome <= Chance of being out) BatsmanIsOut();
else if (Outcome <= Chance of being out + Chance of scoring 6) Score(6);
// etc.


The tricky bit comes in tuning the numbers to make the game play well.

The strategy pattern seems like it'd be a good fit here. It's a way to change algorithms interchangeably at runtime.

I recommend reading up on it via its Wikipedia article or watching the PatternCraft video on the subject: