# ' simulation d'une loi discrete
# ' @export
# ' @param x vecteur numerique representant les valeurs de la variable aleatoire
# ' @param p vecteur numerique representant les probabilit?s
# ' @param n nombre d'iterations
rdist<-function(x,p)
{
n=length(p)
r=runif(1)
b=p[1];
if((r>=0) & (r<=b))
{
y=x[1]
return(y)
}
else
{
a=p[1]
b=b+p[2]
for(i in 2:n-1)
{
if((r>=a) & (r <=b))
{
y=x[i]
return(y)
}
else
{
a=b
b=b+p[i+1]
i=i+1
}
}
y=x[n]
return(y)
}
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.