Skip to content

Instantly share code, notes, and snippets.

@divmgl
Last active January 7, 2017 05:05
Show Gist options
  • Save divmgl/a1509fb85870a8824e5390d6024b5a88 to your computer and use it in GitHub Desktop.
Save divmgl/a1509fb85870a8824e5390d6024b5a88 to your computer and use it in GitHub Desktop.

Revisions

  1. divmgl revised this gist Jan 7, 2017. 1 changed file with 32 additions and 32 deletions.
    64 changes: 32 additions & 32 deletions random.cpp
    Original file line number Diff line number Diff line change
    @@ -11,60 +11,60 @@ using std::array;
    class item
    {
    public:
    string name;
    int weight;
    string name;
    int weight;
    };

    struct descending
    {
    template <class item>
    bool operator()(item const &a, item const &b) const
    {
    return b.weight < a.weight;
    }
    template <class item>
    bool operator()(item const &a, item const &b) const
    {
    return b.weight < a.weight;
    }
    };

    template<std::size_t SIZE>
    item randomize(array<item, SIZE> items)
    {
    int sum = 0;
    std::map<string, double> weights;
    std::random_device rd;
    int sum = 0;
    std::map<string, double> weights;
    std::random_device rd;
    std::mt19937 gen(rd());
    std::uniform_real_distribution<> dis(0, 1);
    double seed = dis(gen);

    for (item current : items)
    {
    sum += current.weight;
    }
    for (item current : items)
    {
    sum += current.weight;
    }

    for (item current : items)
    {
    weights[current.name] = (double)current.weight / sum;
    }
    for (item current : items)
    {
    weights[current.name] = (double)current.weight / sum;
    }

    std::sort(items.begin(), items.end(), descending());
    std::sort(items.begin(), items.end(), descending());

    for (item current : items)
    {
    if (seed < weights[current.name]) return current;
    }
    for (item current : items)
    {
    if (seed < weights[current.name]) return current;
    }

    return items[items.size() - 1];
    return items[items.size() - 1];
    }

    int main()
    {
    array<item, 2> players =
    {{
    { "michael", 10 },
    { "james", 100 }
    }};
    array<item, 2> players =
    {{
    { "michael", 10 },
    { "james", 100 }
    }};

    item player = randomize(players);
    cout << player.name << endl;
    item player = randomize(players);
    cout << player.name << endl;

    return 0;
    return 0;
    }

  2. divmgl revised this gist Jan 7, 2017. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion random.cpp
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,6 @@
    #include <iostream>
    #include <array>
    #include <map>

    #include <random>

    using std::cout;
  3. divmgl created this gist Jan 7, 2017.
    71 changes: 71 additions & 0 deletions random.cpp
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,71 @@
    #include <iostream>
    #include <array>
    #include <map>

    #include <random>

    using std::cout;
    using std::endl;
    using std::string;
    using std::array;

    class item
    {
    public:
    string name;
    int weight;
    };

    struct descending
    {
    template <class item>
    bool operator()(item const &a, item const &b) const
    {
    return b.weight < a.weight;
    }
    };

    template<std::size_t SIZE>
    item randomize(array<item, SIZE> items)
    {
    int sum = 0;
    std::map<string, double> weights;
    std::random_device rd;
    std::mt19937 gen(rd());
    std::uniform_real_distribution<> dis(0, 1);
    double seed = dis(gen);

    for (item current : items)
    {
    sum += current.weight;
    }

    for (item current : items)
    {
    weights[current.name] = (double)current.weight / sum;
    }

    std::sort(items.begin(), items.end(), descending());

    for (item current : items)
    {
    if (seed < weights[current.name]) return current;
    }

    return items[items.size() - 1];
    }

    int main()
    {
    array<item, 2> players =
    {{
    { "michael", 10 },
    { "james", 100 }
    }};

    item player = randomize(players);
    cout << player.name << endl;

    return 0;
    }