Skip to content

Random Extensions

Shmellyorc edited this page Aug 31, 2026 · 1 revision

RandExtensions provides a comprehensive set of extension methods for the FastRandom class, making common random operations more intuitive and expressive.


Overview

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

Element Selection

Choice (Array)

Selects a random element from an array.

var rng = FastRandom.Shared;
var items = new[] { "apple", "banana", "cherry" };

string selected = rng.Choice(items);

Choice (List)

Selects a random element from a read-only list.

var list = new List<string> { "apple", "banana", "cherry" };
string selected = rng.Choice(list);

Choice (Sequence)

Selects a random element from any sequence using reservoir sampling.

IEnumerable<string> items = GetItems();
string selected = rng.Choice(items);

Direction Generation

RandomDirection

Generates a random 4-way direction (Up, Right, Down, Left).

Vect2 direction = rng.RandomDirection();
// Returns Vect2.Up, .Right, .Down, or .Left

RandomDirection8Way

Generates a random 8-way direction including diagonals.

Vect2 direction = rng.RandomDirection8Way();
// Returns any of the 8 cardinal and intercardinal directions

Color Generation

RandomColor

Generates a random RGB color with full value range (0-255).

Color color = rng.RandomColor();
// Returns a color with random R, G, B values

RandomPastelColor

Generates a random pastel color with light values (128-255).

Color pastel = rng.RandomPastelColor();
// Returns a color with light, soft values

RandomDarkColor

Generates a random dark color with low values (0-127).

Color dark = rng.RandomDarkColor();
// Returns a color with dark, muted values

Enum Selection

RandomEnum

Generates 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 value

Sign and Shuffling

NextSign

Generates a random sign (1 or -1).

int sign = rng.NextSign();  // 1 or -1

Shuffle

Shuffles 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 order

Dice Rolling

RollDice

Rolls 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);  // 2d10

Point Generation

RandomPointInCircle (Unit Circle)

Generates a random point uniformly within a unit circle.

Vect2 point = rng.RandomPointInCircle();
// Returns a point within a circle of radius 1

RandomPointInCircle (Custom Radius)

Generates a random point uniformly within a circle of the specified radius.

Vect2 point = rng.RandomPointInCircle(5f);
// Returns a point within a circle of radius 5

Angle Generation

RandomAngle

Generates a random angle in radians (0 to 2π).

float angle = rng.RandomAngle();
// Returns a random angle in radians

RandomAngleDegrees

Generates a random angle in degrees (0 to 360).

float angle = rng.RandomAngleDegrees();
// Returns a random angle in degrees

Probability

Chance

Returns 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!");
}

Examples

Random Enemy Spawning

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);
}

Random Loot Generation

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;
}

Random Weather

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;
    }
}

Random Particle System

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);
    }
}

Random Puzzle Generation

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
    }
}

Summary

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

Back to Home

Clone this wiki locally