API IntRange - shmellyorc/Box GitHub Wiki

IntRange

Namespace: Box.Utils.Ranges

Represents an integer range with an enforced minimum and maximum, and a current value automatically clamped between those bounds. Provides utilities for normalization, random sampling, and range queries.


Constructors

public IntRange(int value, int min, int max)

Create a range where Min = min, Max = max (adjusted so Max ≥ Min), and Value is clamped accordingly.

public IntRange(int value, int max)

Shortcut constructor: Min = 0, Max = max, and Value is clamped.

public IntRange(int value)

Shortcut constructor: Min = 0, Max = value, and Value = max.


Properties

Name Type Description
static IntRange Zero IntRange A range with Min=0, Max=0, Value=0.
bool IsZero bool True when Min, Max, and Value are all zero.
int Min int Minimum bound. Setting above Max adjusts Max = Min. Triggers re-clamp of Value.
int Max int Maximum bound. Setting below Min adjusts Min = Max. Triggers re-clamp of Value.
int Value int Current value, always clamped between Min and Max.
int Sum int Returns Min + Max.
int Total int Returns Max - Min.
float Percent float Normalized position of Value within [Min,Max] (0–1).
bool AtStart bool True if Value is at or below Min.
bool AtEnd bool True if Value is at or above Max.

Methods

Signature Description
int Clamp(int value) Clamp an arbitrary value to the current range.
int Random() Generate a random integer between Min and Max via FastRandom.Instance.
override string ToString() Returns a string in "[Min .. Max] = Value" format.

Usage Example

// Create a range from 5 to 15, initial Value=10
var range = new IntRange(10, 5, 15);

// Access properties
debug.Assert(range.Min == 5);
debug.Assert(range.Max == 15);
debug.Assert(range.Value == 10);

// Clamp assignment
range.Value = 20; // Value becomes 15 (clamped)

// Normalized percent
float p = range.Percent; // (15-5)/(15-5) = 1.0f

// Random sample within the range
int sample = range.Random(); // e.g. 12

// Adjust bounds
tange.Min = 0; // Min=0, Max=15, Value re-clamped if needed

// Utility methods
int c = range.Clamp(-10); // 0
string s = range.ToString(); // "[0 .. 15] = 15"