Generate Integer number between 0 and 26 base on probability

Thread Starter

gary1wang

Joined Sep 18, 2008
23
Hi Guys


How can i generate a integer number between 0 and 26 based on probability by using C++. can you show me an example please.


I have probablity for each number in an array
prob[ 26] = { 0.1 , 0 2. 0.01, 0 , 0.25 , 0 , 0 , 0.20.02,0,0,0,0,0,0,0,0,0,0, 0,0 , 0.23 }

the example array above.

Thanks
 
Last edited:

AsmCoder8088

Joined Apr 17, 2010
15
A basic approach would be to create a "box" of items, proportioning out the items to the set of probabilities. This would yield the desired probabilities as defined by the given set. To make it random, one can then shuffle the contents of the box. The larger the box the more accurate it can be (as in, if you have probabilities such as 0.00001, it would require a box of 100,000 elements to record one occurrence of that label).

Here is some working code:

Rich (BB code):
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

// bigrand - Returns a big random number
int bigrand()
{
    return RAND_MAX*rand() + rand();
}

// randint - Returns a random number in the desired
//  lower (l) and upper (u) bounds
int randint(int l, int u)
{
    return l + bigrand() % (u-l+1);
}

// swap - Swaps two numbers
void swap(int *a, int *b)
{
    int c;

    c = *a;
    *a = *b;
    *b = c;
}

// shuffle - Knuth's implementation of shuffling algorithm
void shuffle(int *a, int n)
{
    int i;

    for (i=0;i<n;i++)
    {
        swap(&a, &a[randint(i,n-1)]);
    }

}

int main(int argc, char *szArgv[])
{
    const double prob[] = { 0.1, // A
                           0.2,  // B
                           0.01, // C
                           0,    // D
                           0.25, // E
                           0,    // F
                           0,    // G
                           0.20, // H
                           0.02, // I
                           0,    // J
                           0,    // K
                           0,    // L
                           0,    // M
                           0,    // N
                           0,    // O
                           0,    // P
                           0,    // Q
                           0,    // R
                           0,    // S
                           0,    // T
                           0,    // U
                           0.22, // V
                           0,    // W
                           0,    // X
                           0,    // Y
                           0 };  // Z
    double sum;
    int *box;
    int chosen_item;
    int max_items;
    int num_items;
    int i;
    int j;
    int k;

    // Seed the random number generator
    srand(time(NULL));

    // Allocate some memory to store the items
    max_items = 10000;
    box = new int [max_items];

    // Compute the sum of probabilities
    sum = 0;
    for (i=0;i<26;i++)
    {
        sum += prob;
    }

    // Quit if probabilities do not add to 1.0
    if (sum != 1.0)
    {
        printf("Error: Probabilities do not add to 1.0; quitting...\n");
        return 0;
    }

    // Proceed to label each item in the box according to
    //  their probabilities
    k = 0;
    for (i=0;i<26;i++)
    {
        num_items = prob * max_items;
        for (j=0;j<num_items;j++)
        {
            box[k++] = i;
        }
    }

    // Shuffle the contents of the box
    shuffle(box, max_items);


    // Now, one can either index into the box at a random position, or,
    //  since the box has had its contents shuffled, can choose to start
    //  at position zero and go from there...
    chosen_item = box[0];

    printf("Chosen letter = %c\n", 'A' + chosen_item);

    return 0;
}
 
Top