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!
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.
In weighted random sampling (WRS) the items are weighted and the probability of each item to be selected is determined by its relative weight.
See ?sample
.
For example:
sample(c(1, 2, 3), size = 100, replace = TRUE, prob = c(0.1, 0.5, 0.4))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With