Skip to content

Instantly share code, notes, and snippets.

@noqisofon
Created December 14, 2024 01:57
Show Gist options
  • Save noqisofon/cebc321cc7039434eb1f47fad7015a9e to your computer and use it in GitHub Desktop.
Save noqisofon/cebc321cc7039434eb1f47fad7015a9e to your computer and use it in GitHub Desktop.
( ノ╹◡◡╹)ノ Raku の `.pick` と `.roll` を C++ で再現してみた
#pragma once
#include <random>
int32_t pick( std::mt19937_64 &engine, int32_t min, int32_t max ) {
std::uniform_int_distribution<> dist( min, max );
return dist( engine );
}
template <class _Populate_Iterator, class _Sample_Iterator, class _Distance, class _Uniform_Random_Bit_Generator>
_Sample_Iterator roll( _Populate_Iterator first,
_Populate_Iterator last,
_Sample_Iterator dest,
_Distance count,
_Uniform_Random_Bit_Generator randomizer ) {
size_t max = std::distance( first, last );
std::uniform_int_distribution<size_t> dist( 0, max - 1 );
for ( _Distance i = 0; i < count; i++ ) {
auto offset = dist( randomizer );
*dest = *( first + offset );
dest++;
}
return dest;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment