Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Weighted random number generation in R

Tags:

r

I'm trying to generate a set of 100 random integers between a fixed range. One could be comprised of 100 numbers between 1 and 3, with specific probabilities of obtaining either 1, 2 and 3.

Any help would be appreciated!

like image 672
Jonathan Avatar asked Mar 28 '12 00:03

Jonathan


People also ask

How do you create a weighted random number?

To generated a random number, weighted with a given probability, you can use a helper table together with a formula based on the RAND and MATCH functions. Notice, we are intentionally shifting the cumulative probability down one row, so that the value in D5 is zero.

What is weighted random sampling?

In weighted random sampling (WRS) the items are weighted and the probability of each item to be selected is determined by its relative weight.


1 Answers

See ?sample.

For example:

sample(c(1, 2, 3), size = 100, replace = TRUE, prob = c(0.1, 0.5, 0.4))
like image 63
mdsumner Avatar answered Oct 02 '22 07:10

mdsumner