Skip to main content
added 181 characters in body
Source Link
Nic Foster
  • 2.9k
  • 18
  • 23

You could take the real difference, and then add/subtract a random amount or percentage from it.

Pardon the pseudocode, I barely know Java:

int difference = Math.abs(enemy.units - player.units);

int thirtyPercentDifference = Math.round(difference * 0.3f);
int randomNegative = (random.nextInt(1) == 1) ? 1 : -1;
int randomDifference = difference + (random.nextInt(thirtyPercentDifference) * randomNegative);

What the above code would do is take that actual difference and then add +/- 0-30% to it. You should be able to use randomDifference as your final result. Let's plug in the numbers in your example as see what we get.

//int difference = Math.abs(enemy.units - player.units);
abs(enemy.units - player.units) = abs(45 - 34) = 9

//int thirtyPercentDifference = Math.round(difference * 0.3f);
9 * 0.3f = 2.7f, rounded to nearest int = 3

//int randomNegative = (random.nextInt(1) == 1) ? 1 : -1;
Then we randomly get a number either 0 or 1, let's just say it is zero. In that case our randomNegative is -1.

//int randomDifference = difference + (random.nextInt(thirtyPercentDifference) * randomNegative);
9 + ((random number between 0 and 3) * -1) = A number between 6 and 9.

The full range of what we could have gotten is 6 - 12 with the above algorithm. And if you wanted it even more random you could randomize the percentage used in place of the hard-coded 0.3f.

You could take the real difference, and then add/subtract a random amount or percentage from it.

Pardon the pseudocode, I barely know Java:

int difference = Math.abs(enemy.units - player.units);

int thirtyPercentDifference = Math.round(difference * 0.3f);
int randomNegative = (random.nextInt(1) == 1) ? 1 : -1;
int randomDifference = difference + (random.nextInt(thirtyPercentDifference) * randomNegative);

What the above code would do is take that actual difference and then add +/- 0-30% to it. Let's plug in the numbers in your example as see what we get.

//int difference = Math.abs(enemy.units - player.units);
abs(enemy.units - player.units) = abs(45 - 34) = 9

//int thirtyPercentDifference = Math.round(difference * 0.3f);
9 * 0.3f = 2.7f, rounded to nearest int = 3

//int randomNegative = (random.nextInt(1) == 1) ? 1 : -1;
Then we randomly get a number either 0 or 1, let's just say it is zero. In that case our randomNegative is -1.

//int randomDifference = difference + (random.nextInt(thirtyPercentDifference) * randomNegative);
9 + ((random number between 0 and 3) * -1) = A number between 6 and 9.

The full range of what we could have gotten is 6 - 12 with the above algorithm.

You could take the real difference, and then add/subtract a random amount or percentage from it.

Pardon the pseudocode, I barely know Java:

int difference = Math.abs(enemy.units - player.units);

int thirtyPercentDifference = Math.round(difference * 0.3f);
int randomNegative = (random.nextInt(1) == 1) ? 1 : -1;
int randomDifference = difference + (random.nextInt(thirtyPercentDifference) * randomNegative);

What the above code would do is take that actual difference and then add +/- 0-30% to it. You should be able to use randomDifference as your final result. Let's plug in the numbers in your example as see what we get.

//int difference = Math.abs(enemy.units - player.units);
abs(enemy.units - player.units) = abs(45 - 34) = 9

//int thirtyPercentDifference = Math.round(difference * 0.3f);
9 * 0.3f = 2.7f, rounded to nearest int = 3

//int randomNegative = (random.nextInt(1) == 1) ? 1 : -1;
Then we randomly get a number either 0 or 1, let's just say it is zero. In that case our randomNegative is -1.

//int randomDifference = difference + (random.nextInt(thirtyPercentDifference) * randomNegative);
9 + ((random number between 0 and 3) * -1) = A number between 6 and 9.

The full range of what we could have gotten is 6 - 12 with the above algorithm. And if you wanted it even more random you could randomize the percentage used in place of the hard-coded 0.3f.

Source Link
Nic Foster
  • 2.9k
  • 18
  • 23

You could take the real difference, and then add/subtract a random amount or percentage from it.

Pardon the pseudocode, I barely know Java:

int difference = Math.abs(enemy.units - player.units);

int thirtyPercentDifference = Math.round(difference * 0.3f);
int randomNegative = (random.nextInt(1) == 1) ? 1 : -1;
int randomDifference = difference + (random.nextInt(thirtyPercentDifference) * randomNegative);

What the above code would do is take that actual difference and then add +/- 0-30% to it. Let's plug in the numbers in your example as see what we get.

//int difference = Math.abs(enemy.units - player.units);
abs(enemy.units - player.units) = abs(45 - 34) = 9

//int thirtyPercentDifference = Math.round(difference * 0.3f);
9 * 0.3f = 2.7f, rounded to nearest int = 3

//int randomNegative = (random.nextInt(1) == 1) ? 1 : -1;
Then we randomly get a number either 0 or 1, let's just say it is zero. In that case our randomNegative is -1.

//int randomDifference = difference + (random.nextInt(thirtyPercentDifference) * randomNegative);
9 + ((random number between 0 and 3) * -1) = A number between 6 and 9.

The full range of what we could have gotten is 6 - 12 with the above algorithm.