Skip to content

Instantly share code, notes, and snippets.

@thypon
Created September 29, 2015 22:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thypon/18ca1fe6a038ffeac62e to your computer and use it in GitHub Desktop.
Save thypon/18ca1fe6a038ffeac62e to your computer and use it in GitHub Desktop.
wheel extraction
#include <tuple>
#include <list>
#include <string>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <thread>
#define ENTRY pair<string, unsigned int>
#define LABEL 0
#define WEIGHT 1
using namespace std;
int main(int argc, char const *argv[]) {
list<ENTRY> l;
int weight, max = 0;
string label;
srand(time(0));
for (cin >> weight; weight > 0; cin >> weight) {
max += weight;
cin >> label;
l.push_back(make_pair(label, weight));
}
int extract = rand() % max + 1;
cout << extract << endl;
list<ENTRY>::iterator it = l.begin();
extract -= get<WEIGHT>(*it);
while (extract > 0) {
extract -= get<WEIGHT>(* ++it);
}
cout << get<LABEL>(*it) << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment