Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > random number generation

Reply
Thread Tools

random number generation

 
 
Bo Sun
Guest
Posts: n/a
 
      06-12-2004
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
 
Reply With Quote
 
 
 
 
Nudge
Guest
Posts: n/a
 
      06-12-2004
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;

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Math.random() and Math.round(Math.random()) and Math.floor(Math.random()*2) VK Javascript 15 05-02-2010 03:43 PM
Random Number Generation dpi VHDL 4 03-26-2010 10:31 AM
random.random(), random not defined!? globalrev Python 4 04-20-2008 08:12 AM
Need Help With Random Number Generation Between Upper and Lower Bound ANM Java 2 03-07-2004 07:18 AM
random number generation and Monte Carlo simulation in C++ mescaline C++ 4 09-10-2003 09:01 PM



Advertisments