fork(1) download
  1. #include <iostream>
  2. #include <utility>
  3. #include <vector>
  4. #include <random>
  5. #include <chrono>
  6.  
  7. //а щоб ви мені здорові були
  8. std::default_random_engine generator( std::chrono::system_clock::now().time_since_epoch().count() );
  9.  
  10.  
  11. int irregularRand( std::vector< std::pair< int, int > > &probabilities ) {
  12. int sum = 0;
  13. for( auto &p : probabilities ) {
  14. sum += p.first;
  15. }
  16. std::uniform_int_distribution<int> distribution( 0, sum - 1);
  17. int rnd = distribution( generator );
  18. for( auto &p : probabilities ) {
  19. rnd -= p.first;
  20. if( rnd < 0 ) {
  21. return p.second;
  22. }
  23. }
  24. return -1;//щось дуже сильно не так
  25. }
  26.  
  27. int main() {
  28. std::vector< std::pair< int, int > > p
  29. = { std::make_pair( 900, 1),
  30. std::make_pair( 80, 2),
  31. std::make_pair( 19, 3),
  32. std::make_pair( 1, 4) };
  33. for( int i = 0; i < 1000 ; ++i )
  34. std::cout << irregularRand( p ) << " " ;
  35. return 0;
  36. }
Success #stdin #stdout 0s 3228KB
stdin
Standard input is empty
stdout
1 1 2 1 1 3 2 1 1 1 1 1 1 1 1 1 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 3 1 1 1 1 1 1 1 1 2 1 1 1 1 1 1 1 4 1 1 1 1 1 1 1 2 1 1 1 1 1 1 1 3 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 1 1 1 1 1 1 1 1 1 1 1 2 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 1 1 1 1 1 1 1 2 1 1 1 1 1 1 2 1 1 2 1 1 1 3 1 1 1 1 1 1 1 1 1 1 1 1 2 1 1 1 2 1 1 2 1 1 1 1 1 1 1 1 1 2 1 1 1 1 2 1 1 1 1 1 1 1 1 1 1 1 1 2 2 1 1 1 1 2 1 1 1 1 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