Generate Integer number between 0 and 26 base on probability

Discussion in 'Programmer's Corner' started by gary1wang, Feb 21, 2012.

  1. gary1wang

    Thread Starter Member

    Sep 18, 2008
    23
    0
    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: Feb 21, 2012
  2. AsmCoder8088

    New Member

    Apr 17, 2010
    15
    1
    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:

    Code ( (Unknown Language)):
    1.  
    2. #include <stdio.h>
    3. #include <stdlib.h>
    4. #include <time.h>
    5.  
    6. // bigrand - Returns a big random number
    7. int bigrand()
    8. {
    9.     return RAND_MAX*rand() + rand();
    10. }
    11.  
    12. // randint - Returns a random number in the desired
    13. //  lower (l) and upper (u) bounds
    14. int randint(int l, int u)
    15. {
    16.     return l + bigrand() % (u-l+1);
    17. }
    18.  
    19. // swap - Swaps two numbers
    20. void swap(int *a, int *b)
    21. {
    22.     int c;
    23.  
    24.     c = *a;
    25.     *a = *b;
    26.     *b = c;
    27. }
    28.  
    29. // shuffle - Knuth's implementation of shuffling algorithm
    30. void shuffle(int *a, int n)
    31. {
    32.     int i;
    33.  
    34.     for (i=0;i<n;i++)
    35.     {
    36.         swap(&a[i], &a[randint(i,n-1)]);
    37.     }
    38.  
    39. }
    40.  
    41. int main(int argc, char *szArgv[])
    42. {
    43.     const double prob[] = { 0.1, // A
    44.                            0.2,  // B
    45.                            0.01, // C
    46.                            0,    // D
    47.                            0.25, // E
    48.                            0,    // F
    49.                            0,    // G
    50.                            0.20, // H
    51.                            0.02, // I
    52.                            0,    // J
    53.                            0,    // K
    54.                            0,    // L
    55.                            0,    // M
    56.                            0,    // N
    57.                            0,    // O
    58.                            0,    // P
    59.                            0,    // Q
    60.                            0,    // R
    61.                            0,    // S
    62.                            0,    // T
    63.                            0,    // U
    64.                            0.22, // V
    65.                            0,    // W
    66.                            0,    // X
    67.                            0,    // Y
    68.                            0 };  // Z
    69.     double sum;
    70.     int *box;
    71.     int chosen_item;
    72.     int max_items;
    73.     int num_items;
    74.     int i;
    75.     int j;
    76.     int k;
    77.  
    78.     // Seed the random number generator
    79.     srand(time(NULL));
    80.  
    81.     // Allocate some memory to store the items
    82.     max_items = 10000;
    83.     box = new int [max_items];
    84.  
    85.     // Compute the sum of probabilities
    86.     sum = 0;
    87.     for (i=0;i<26;i++)
    88.     {
    89.         sum += prob[i];
    90.     }
    91.  
    92.     // Quit if probabilities do not add to 1.0
    93.     if (sum != 1.0)
    94.     {
    95.         printf("Error: Probabilities do not add to 1.0; quitting...\n");
    96.         return 0;
    97.     }
    98.  
    99.     // Proceed to label each item in the box according to
    100.     //  their probabilities
    101.     k = 0;
    102.     for (i=0;i<26;i++)
    103.     {
    104.         num_items = prob[i] * max_items;
    105.         for (j=0;j<num_items;j++)
    106.         {
    107.             box[k++] = i;
    108.         }
    109.     }
    110.  
    111.     // Shuffle the contents of the box
    112.     shuffle(box, max_items);
    113.  
    114.  
    115.     // Now, one can either index into the box at a random position, or,
    116.     //  since the box has had its contents shuffled, can choose to start
    117.     //  at position zero and go from there...
    118.     chosen_item = box[0];
    119.  
    120.     printf("Chosen letter = %c\n", 'A' + chosen_item);
    121.  
    122.     return 0;
    123. }
    124. [/i][/i][/i]
     
Loading...