Created
December 14, 2024 01:57
-
-
Save noqisofon/cebc321cc7039434eb1f47fad7015a9e to your computer and use it in GitHub Desktop.
( ノ╹◡◡╹)ノ Raku の `.pick` と `.roll` を C++ で再現してみた
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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