Skip to content

Instantly share code, notes, and snippets.

@MaksimSazanovich
Last active June 28, 2024 15:07
Show Gist options
  • Save MaksimSazanovich/d08d7ddd8fe6b48ebe5f8a08b6f83ba1 to your computer and use it in GitHub Desktop.
Save MaksimSazanovich/d08d7ddd8fe6b48ebe5f8a08b6f83ba1 to your computer and use it in GitHub Desktop.
My modified version of Random class in C# which returns smaller values more often than bigger ones.
using UnityEngine;
namespace Unity_one_love
{
public static class HierarchyRandom
{
public static int Range(int minValue, int maxValue)
{
int firstSum = maxValue - minValue + 1;
int sum = firstSum * (firstSum + 1) / 2 + 1;
int randomValue = Random.Range(1, sum);
int min = 0;
int max = 1;
for (int i = 1; i <= firstSum + 1; i++)
{
min += i;
max = min + i + 1;
if (randomValue == 1)
{
return maxValue;
}
if (randomValue >= min && randomValue <= max)
{
int group = i + 1;
return maxValue - group + 1;
}
}
return 0;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment