-
Notifications
You must be signed in to change notification settings - Fork 1
Random Extensions
RandExtensions provides a comprehensive set of extension methods for the FastRandom class, making common random operations more intuitive and expressive.
| Feature | Description |
|---|---|
| Element Selection | Random selection from arrays, lists, and sequences |
| Direction Generation | 4-way and 8-way random directions |
| Color Generation | Random, pastel, and dark colors |
| Enum Selection | Random enum values with caching |
| Sign | Random sign (1 or -1) |
| Shuffling | Shuffle lists in-place |
| Dice Rolling | Roll multiple dice and sum results |
| Point Generation | Random points within a circle |
| Angles | Random angles in radians or degrees |
| Probability | Chance checks with any probability |
Selects a random element from an array.
var rng = FastRandom.Shared;
var items = new[] { "apple", "banana", "cherry" };
string selected = rng.Choice(items);Selects a random element from a read-only list.
var list = new List<string> { "apple", "banana", "cherry" };
string selected = rng.Choice(list);Selects a random element from any sequence using reservoir sampling.
IEnumerable<string> items = GetItems();
string selected = rng.Choice(items);Generates a random 4-way direction (Up, Right, Down, Left).
Vect2 direction = rng.RandomDirection();
// Returns Vect2.Up, .Right, .Down, or .LeftGenerates a random 8-way direction including diagonals.
Vect2 direction = rng.RandomDirection8Way();
// Returns any of the 8 cardinal and intercardinal directionsGenerates a random RGB color with full value range (0-255).
Color color = rng.RandomColor();
// Returns a color with random R, G, B valuesGenerates a random pastel color with light values (128-255).
Color pastel = rng.RandomPastelColor();
// Returns a color with light, soft valuesGenerates a random dark color with low values (0-127).
Color dark = rng.RandomDarkColor();
// Returns a color with dark, muted valuesGenerates a random value from the specified enum type with caching.
public enum EnemyType { Goblin, Orc, Troll, Dragon }
EnemyType type = rng.RandomEnum<EnemyType>();
// Returns a random enum valueGenerates a random sign (1 or -1).
int sign = rng.NextSign(); // 1 or -1Shuffles a list in-place using the Fisher-Yates algorithm.
var list = new List<int> { 1, 2, 3, 4, 5 };
rng.Shuffle(list);
// list is now in random orderRolls dice and returns the sum.
// Roll 3 six-sided dice
int sum = rng.RollDice(3, 6); // 3d6, sum between 3 and 18
// Roll 2 ten-sided dice
int sum2 = rng.RollDice(2, 10); // 2d10Generates a random point uniformly within a unit circle.
Vect2 point = rng.RandomPointInCircle();
// Returns a point within a circle of radius 1Generates a random point uniformly within a circle of the specified radius.
Vect2 point = rng.RandomPointInCircle(5f);
// Returns a point within a circle of radius 5Generates a random angle in radians (0 to 2π).
float angle = rng.RandomAngle();
// Returns a random angle in radiansGenerates a random angle in degrees (0 to 360).
float angle = rng.RandomAngleDegrees();
// Returns a random angle in degreesReturns true with the specified probability.
// 25% chance
if (rng.Chance(0.25f))
{
Console.WriteLine("Lucky!");
}
// 50% chance
if (rng.Chance(0.5f))
{
Console.WriteLine("Heads!");
}
else
{
Console.WriteLine("Tails!");
}public Enemy SpawnEnemy(Vect2 position)
{
var rng = FastRandom.Shared;
// Random enemy type
EnemyType type = rng.RandomEnum<EnemyType>();
// Random direction to face
Vect2 direction = rng.RandomDirection();
// Random color variation
Color color = rng.RandomPastelColor();
// Random position offset within range
Vect2 offset = rng.RandomPointInCircle(50f);
return EnemyFactory.Create(type, position + offset, direction, color);
}public List<Item> GenerateLoot()
{
var rng = FastRandom.Shared;
var loot = new List<Item>();
// Random amount of items (1-5)
int count = rng.RollDice(1, 5);
var allItems = _itemDatabase.GetAllItems();
for (int i = 0; i < count; i++)
{
var item = rng.Choice(allItems);
loot.Add(item);
}
return loot;
}public void UpdateWeather()
{
var rng = FastRandom.Shared;
// 30% chance of rain
if (rng.Chance(0.3f))
{
_currentWeather = Weather.Rainy;
}
// 20% chance of snow
else if (rng.Chance(0.2f))
{
_currentWeather = Weather.Snowy;
}
else
{
_currentWeather = Weather.Clear;
}
}public void EmitParticles(Vect2 position, int count)
{
var rng = FastRandom.Shared;
for (int i = 0; i < count; i++)
{
// Random velocity direction
Vect2 direction = rng.RandomDirection8Way();
float speed = rng.RangeFloat(50f, 200f);
Vect2 velocity = direction * speed;
// Random color variation
Color color = rng.RandomColor();
// Random lifespan
float lifespan = rng.RangeFloat(0.5f, 2f);
_particleSystem.Emit(position, velocity, color, lifespan);
}
}public void GeneratePuzzle()
{
var rng = FastRandom.Shared;
// Random symbols
var symbols = new[] { 'A', 'B', 'C', 'D', 'E' };
// Shuffle the sequence
var sequence = symbols.ToList();
rng.Shuffle(sequence);
// Random starting position
int startIndex = rng.Next(symbols.Length);
// Random rotation for each symbol
foreach (var symbol in sequence)
{
float rotation = rng.RandomAngleDegrees();
// Apply rotation
}
}| Method | Description |
|---|---|
Choice |
Selects a random element |
RandomDirection |
Returns a random 4-way direction |
RandomDirection8Way |
Returns a random 8-way direction |
RandomColor |
Returns a random color |
RandomPastelColor |
Returns a random pastel color |
RandomDarkColor |
Returns a random dark color |
RandomEnum |
Returns a random enum value |
NextSign |
Returns 1 or -1 |
Shuffle |
Shuffles a list in-place |
RollDice |
Rolls dice and returns the sum |
RandomPointInCircle |
Returns a random point within a circle |
RandomAngle |
Returns a random angle in radians |
RandomAngleDegrees |
Returns a random angle in degrees |
Chance |
Returns true with a given probability |
- MathHelper
- FileHelper
- HashHelper
- JsonHelper
- MapHelper
- TextHelper
- SoundHelper
- AlignHelpers
- Instance Helper
- Enum Extensions
- String Extensions
- Int Extensions
- Float Extensions
- IEnumerable Extensions
- Random Extensions
- Sound Extensions
- Font Extensions