Skip to content

Instantly share code, notes, and snippets.

@nomnomab
Last active June 21, 2021 20:30
Show Gist options
  • Save nomnomab/d6016edeb881b9e90cb933ae3fb1a8d0 to your computer and use it in GitHub Desktop.
Save nomnomab/d6016edeb881b9e90cb933ae3fb1a8d0 to your computer and use it in GitHub Desktop.
Random number in a range whilst excluding an inner range
public static int RandomNumberInRange(int min, int max, int minExclusion, int maxExclusion) {
int value = Random.Range(min, max + 1 - maxExclusion + minExclusion);
value += value >= minExclusion ? maxExclusion - minExclusion : 0;
return value;
}
@nomnomab
Copy link
Author

nomnomab commented Jun 21, 2021

// generates a random number between 1 and 10 while values between 3 and 6
int number = RandomNumberInRange(1, 10, 3, 6);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment