Skip to content

Latest commit

 

History

28 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 

Repository files navigation

About

"Random" is a modern, header-only C++20 library that simplifies random number generation with a clean, intuitive interface. It leverages C++20 features like concepts, ranges, and uniform distribution to provide type-safe, efficient random operations.

Key Features

  • C++20 Concepts: Type-safe range operations with ArithmeticRange concept
  • Range Support: Fill and shuffle standard containers (std::vector, std::array, etc.)
  • Thread Safety: Thread-local storage provides automatic thread isolation (no locking overhead)
  • Unified Template API: Single templated Next method works with any arithmetic type
  • Automatic Type Detection: Distributions automatically adapt to integer vs floating-point types
  • Modern API: Intuitive method names and parameter ordering
  • Header-only: No compilation needed, just include and use

Examples

Setup

Initialize the Random instance with a seed generated from std::random_device.

Random random;

Generating Numbers

Use the unified Next method for both integers and floating-point types.

// Integers
int i1 = random.Next(50);            // Range: [0, 50)
int i2 = random.Next(1, 100);        // Range: [1, 100)

// Floating-point
float f1 = random.Next(0.0f, 1.0f);
double d1 = random.Next();           // Range: [0.0, 1.0)
double d2 = random.Next(1e3);        // Range: [0.0, 1e3)

Filling and Shuffling

Easily populate or reorder standard containers.

// Fill a vector with random numbers between -10 and 10
std::vector<int> vec(10);
random.Fill(vec, -10, 10);

// Shuffle an existing container
std::vector<int> numbers = {1, 2, 3, 4, 5};
random.Shuffle(numbers);

Selecting Random Items

Extract elements from any standard range or container.

std::array<std::string, 4> choices = { "Anna", "Jane", "Felix", "Max" };

// Get a single random item
std::string name = random.GetItem(choices);

// Get multiple items (compile-time size)
std::array<std::string, 2> two_names = random.GetItems<2>(choices);

// Get multiple items (runtime size)
std::vector<std::string> dynamic_names = random.GetItems(choices, 2);

You can also direct random selections straight into a pre-allocated destination container.

std::vector<std::string> destination(4);
random.GetItems(choices, destination);

Generating Random Vectors

Quickly generate a completely new std::vector populated with random values in a single line.

// Generate a vector of 100 integers ranging from -10 to 10
std::vector<int> random_vec = random.RandomVector<int>(-10, 10, 100);

About

A modern, header-only C++20 random library based on <random> with range support and thread-safe operations

Topics

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages