#include <iostream>
#include <utility>
#include <vector>
#include <random>
#include <chrono>
//а щоб ви мені здорові були
std::default_random_engine generator( std::chrono::system_clock::now().time_since_epoch().count() );
int irregularRand( std::vector< std::pair< int, int > > &probabilities ) {
int sum = 0;
for( auto &p : probabilities ) {
sum += p.first;
}
std::uniform_int_distribution<int> distribution( 0, sum - 1);
int rnd = distribution( generator );
for( auto &p : probabilities ) {
rnd -= p.first;
if( rnd < 0 ) {
return p.second;
}
}
return -1;//щось дуже сильно не так
}
int main() {
std::vector< std::pair< int, int > > p
= { std::make_pair( 900, 1),
std::make_pair( 80, 2),
std::make_pair( 19, 3),
std::make_pair( 1, 4) };
for( int i = 0; i < 1000 ; ++i )
std::cout << irregularRand( p ) << " " ;
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8dXRpbGl0eT4KI2luY2x1ZGUgPHZlY3Rvcj4KI2luY2x1ZGUgPHJhbmRvbT4KI2luY2x1ZGUgPGNocm9ubz4KCi8v0LAg0YnQvtCxINCy0Lgg0LzQtdC90ZYg0LfQtNC+0YDQvtCy0ZYg0LHRg9C70LgKc3RkOjpkZWZhdWx0X3JhbmRvbV9lbmdpbmUgZ2VuZXJhdG9yKCBzdGQ6OmNocm9ubzo6c3lzdGVtX2Nsb2NrOjpub3coKS50aW1lX3NpbmNlX2Vwb2NoKCkuY291bnQoKSApOwogIAoKaW50IGlycmVndWxhclJhbmQoIHN0ZDo6dmVjdG9yPCBzdGQ6OnBhaXI8IGludCwgaW50ID4gPiAmcHJvYmFiaWxpdGllcyApIHsKICBpbnQgc3VtID0gMDsKICBmb3IoIGF1dG8gJnAgOiBwcm9iYWJpbGl0aWVzICkgewogIAlzdW0gKz0gcC5maXJzdDsKICB9CiAgc3RkOjp1bmlmb3JtX2ludF9kaXN0cmlidXRpb248aW50PiBkaXN0cmlidXRpb24oIDAsIHN1bSAtIDEpOwogIGludCBybmQgPSBkaXN0cmlidXRpb24oIGdlbmVyYXRvciApOwogIGZvcigg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