How can one choose a number with a specific probability p?

Say we must choose between {0, 1} and the probability p stands for choosing 1.

So when p=0.8 we choose 1 with 80% and 0 with 20%.

Is there a simple solution in R for this? Mar 7 19 views

## 1 answer to this question.

Examine the sample function.

```set.seed(1)
sample(c(0,1), size=12, replace=TRUE, prob=c(0.2,0.8))

1 1 1 0 1 0 0 1 1 1 1 1 ```

sample takes a sample of the specified size from the components of x and in sample,
and the argument prob in sample is a vector of probability weights for obtaining the elements of the sampled vector. answered Mar 14 by
• 6,000 points

## Random numbers with user-defined continuous probability distribution

You can use the rejection sampling approach ...READ MORE

## Create random numbers with left skewed probability distribution

I would like to pick a number ...READ MORE

## Simple algorithm for generating random numbers with bigger/smaller probability

To move the density in one way, ...READ MORE

## How to sample n random rows per group in a dataframe?

You can assign a random ID to ...READ MORE

## Big Data transformations with R

Dear Koushik, Hope you are doing great. You can ...READ MORE

## Finding frequency of observations in R

You can use the "dplyr" package to ...READ MORE