NEED HELP: random numbers

Ask for help with your homework/assignments in this forum!

Moderators: Darobat, RecursiveS, Dante Shamest, Bugdude, Wizard

NEED HELP: random numbers

Postby helpme » Mon Jan 12, 2004 4:33 pm

rand() generates a random number with equal probability i believe right?
how can I generate uniform random variable [0, 1] values with different probabilites?

example: probability p=0.9 being true(value being 1) and p=0.1 being false(value being 0)
helpme
 

Postby Alvaro » Mon Jan 12, 2004 8:16 pm

What you are asking for is not a uniform random variable. You want a biased coin (a.k.a. Bernoulli distribution).

Code: Select all
double uniform(void){
   return (double)std::rand()/RAND_MAX;
}

int bernoulli(double p){
   return uniform()<p;
}

User avatar
Alvaro
Moderator
 
Posts: 5185
Joined: Mon Sep 22, 2003 4:57 pm
Location: NY, USA


Return to Homeworks

Who is online

Users browsing this forum: Google Adsense [Bot] and 1 guest