Boost logo

Boost :

From: AngleWyrm (anglewyrm_at_[hidden])
Date: 2004-09-03 23:39:20


----- Original Message -----
From: "Joel Young" <jdy_at_[hidden]>
To: "AngleWyrm" <anglewyrm_at_[hidden]>
Cc: <jdy_at_[hidden]>; <boost_at_[hidden]>
Sent: Friday, September 03, 2004 7:32 PM
Subject: Re: C++ Random Container
> Its
> me say:
> >> priority. The head/front/pop operator would
> >> then select based on the distribution.
>
> Wyrm Say:
> > order? Could you give an example of a few lines of code that demonstrate
the
> > usage?
>
> It would be used exactly like a priority queue, except that the
> next item returned would be selected randomly, either by some
> weight based on the priority (one case, bout like you have)
> or based on its order. If it was one from the front then
> 50 percent chance, 2 from the front 25% and so on. (just an
> example)

One way this could be accomplished is like so:

// Near Top
#include <cstdlib>
#include <iostream>
#include "hat.h"
using namespace std;

int main()
{
    // some alphabetical names; we mostly want near the beginning
    string names[7] = {
      "Amy", "Betty", "Carol", "Debbie", "Ethyl", "Freda", "Gertrude"
    };

    // build a hat of offsets, with a high probability of a low offset
    hat<int> offsets;
    float data[7] = { 0.60, 0.20, 0.10, 0.05, 0.03, 0.01, 0.01 };
    for( int i=0; i<7; i++){
        offsets.put_normalized( i, data[i] );
    }

    // initialize random number generator
    srand( time(NULL) ); rand();

    cout << names[ offsets.get() ] << endl;

    system("pause");
}


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk