random number generation

Discussion in 'C Programming' started by Bo Sun, Jun 12, 2004.

  1. Bo Sun

    Bo Sun Guest

    hi,

    I would like to code the following problem:

    there are 4 events, and their happening probability is:

    e1 with probability 0.5
    e2 with probability 0.2
    e3 with probability 0.2
    e4 with probability 0.1

    I want to use some random function generation function. when its return
    value is [0. 0.5), I select e1, when it is [0.5, 0.75), I select e2..

    My question is: which function should I use to generate [0, 0.5).. based
    on the probability? How should I code this?

    Many thanks,

    Bo
     
    Bo Sun, Jun 12, 2004
    #1
    1. Advertising

  2. Bo Sun

    Nudge Guest

    Bo Sun wrote:

    > I would like to code the following problem:
    >
    > there are 4 events, and their happening probability is:
    >
    > e1 with probability 0.5
    > e2 with probability 0.2
    > e3 with probability 0.2
    > e4 with probability 0.1
    >
    > I want to use some random function generation function. when its return
    > value is [0. 0.5), I select e1, when it is [0.5, 0.75), I select e2..
    >
    > My question is: which function should I use to generate [0, 0.5).. based
    > on the probability? How should I code this?


    #include <stdlib.h>

    double p = (double)rand() / (double)RAND_MAX;

    if (p < 0.5) return e1;
    if (p < 0.7) return e2;
    if (p < 0.9) return e3;
    else return e4;
     
    Nudge, Jun 12, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. dpi

    Random Number Generation

    dpi, Mar 13, 2006, in forum: VHDL
    Replies:
    4
    Views:
    10,090
    vipinlal
    Mar 26, 2010
  2. ANM
    Replies:
    2
    Views:
    1,371
    Thomas Schodt
    Mar 7, 2004
  3. mescaline
    Replies:
    4
    Views:
    1,342
    Cy Edmunds
    Sep 10, 2003
  4. globalrev
    Replies:
    4
    Views:
    774
    Gabriel Genellina
    Apr 20, 2008
  5. VK
    Replies:
    15
    Views:
    1,180
    Dr J R Stockton
    May 2, 2010
Loading...

Share This Page