/random-permutation

Generating random permutations on the fly in C++20.

Primary LanguageC++MIT LicenseMIT

Generating Random Permutations in C++

This small header-only C++20 library provides a pseudo-random permutation generator.

The key property of a permutation is that every number in it occurs exactly once, i.e., this can be used to generate a sequence of random numbers without repetitions. It can be seeded to reproduce the same permutation multiple times at will.

Generation is done on the fly: other than the size of the universe and the random seed and a prime, nothing needs to be saved. This allows generating only a portion of a potentially very large permutation with no additional memory overhead whatsoever. Permuting a number involves only few arithmetic operations and is thus very fast. The only time consuming part (in the order of few milliseconds) is the one-time initialization that must find a suitable prime – see how it works.

This repository also comes with a handy command line tool.

Requirements

The library and command line tool are written in C++20, a corresponding compiler is required. Tests have been done only with GCC 11.

If you desire to use the command line tool, you need CMake.

License

MIT License

Copyright (c) 2022 Patrick Dinklage

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

How it works

The generator is based on an article by Jeff Preshing, who described how to generate random permutations of 32-bit numbers using quadratic residues of primes.

This library extends the idea to support an arbitrary universe size of up to 2^64-1. It requires finding the largest prime that (1) lies within the universe and (2) satisfies (3 mod 4). The prime search is implemented rather naively in this library using a folklore algorithm, but is still reasonably fast for all possible universes.

Randomness is achieved by applying a two-phase permutation: we first permute the original number, and then permute the permuted value offset by the random seed. The outcome is reasonably well distributed.

Usage

The library is header only, so all you need to do is make sure it's in your include path and:

#include <random_permutation.hpp>

In case you use CMake, you can embed this repository into yours (e.g., as a git submodule) and add it like so:

add_subdirectory(path/to/random-permutation)

You can then link against the random-permutation interface library, which will automatically add the include directory to your target.

API

The API is very straightforward. Here's an example that generates a random permutation of one hundred 16-bit numbers:

#include <iostream>
#include "random_permutation.hpp"

int main(int argc, char** argv) {
    // print the first 100 numbers from a random permutation of [0, 2^16-1]
    auto perm = random_permutation::RandomPermutation(UINT16_MAX);
    for(unsigned i = 0; i < 100; i++) {
        std::cout << perm(x) << std::endl;
    }
    return 0;
}

The system's high resolution timestamp is used as the random seed by default, which can be acquired by calling random_permutation::timestamp(). You can also provide an explicit random seed as the second parameter to the constructor.

The RandomPermutation class also provides the STL-style iterators begin and end, allowing ranged for loops:

#include <random_permutation.hpp>
#include <iostream>

int main(int argc, char** argv) {
    // print a random permutation of the numbers from 0 to 100
    auto perm = random_permutation::RandomPermutation(100);
    for(auto x : perm) std::cout << x << std::endl;
    return 0;
}

You can also use the at iterator to start or stop at a certain point.

Command Line Tool

If you need a permutation in a file, you can use the provided command-line tool powered by the tlx command line parser.

Build it as follows:

git clone https://github.com/pdinklag/random-permutation.git
cd random-permutation
mkdir build; cd build
cmake ..
make generate

You may then run:

src/generate --help

The output is one number per line in the standard output; process it as needed.