diff --git a/concepts/randomness/.meta/config.json b/concepts/randomness/.meta/config.json new file mode 100644 index 00000000..4f31b6dc --- /dev/null +++ b/concepts/randomness/.meta/config.json @@ -0,0 +1,5 @@ +{ + "authors": ["colinleach"], + "contributors": [], + "blurb": "R offers a rich variety of ways to generate random values, for many statistical distributions." +} \ No newline at end of file diff --git a/concepts/randomness/about.md b/concepts/randomness/about.md new file mode 100644 index 00000000..a520a556 --- /dev/null +++ b/concepts/randomness/about.md @@ -0,0 +1,39 @@ +# About + +R was derived from a statistics package and is often used for simulations, so it offers a rich variety of ways to generate random values. +These are a few common ones. + +## Discrete values with `sample` + +To pick from a limited number of discrete values, `sample()` can be used with or without replacement (i.e. is it permitted to get repeated values?). +This is useful for integers, strings, etc. + +```R +> sample(1:10, 5) # no repeats +[1] 8 3 4 2 6 + +> sample(1:10, 5, replace = TRUE) # repeats are OK +[1] 10 10 7 10 9 + +> languages <- c("Fortran", "R", "Python", "Julia") +> sample(languages, 1) +[1] "Python" + +> sample(c(TRUE, FALSE), 5, replace = TRUE) # coin flips +[1] FALSE TRUE FALSE TRUE TRUE +``` + +## Floating-point numbers + +For a random-uniform distribution use `runif()`. +Both `min` and `max` can be specified but default to 0 and 1. + +```R +> runif(5) +[1] 0.3038506 0.3527959 0.3319309 0.4846354 0.4386279 + +> runif(5, max = 100) +[1] 79.70762 51.62232 52.85281 71.08571 63.94380 +``` + +Other common distributions include `rnorm()` for Gaussian, `rbinom()` for binomial, `rpois()` for Poisson, etc. diff --git a/concepts/randomness/introduction.md b/concepts/randomness/introduction.md new file mode 100644 index 00000000..6230d9d7 --- /dev/null +++ b/concepts/randomness/introduction.md @@ -0,0 +1,36 @@ +# Introduction + +There are many options for generating random values, and these are a few common ones. + +## Discrete values with `sample` + +To pick from a limited number of discrete values, `sample()` can be used with or without replacement (i.e. is it permitted to get repeated values?). +This is useful for integers, strings, etc. + +```R +> sample(1:10, 5) # no repeats +[1] 8 3 4 2 6 + +> sample(1:10, 5, replace = TRUE) # repeats are OK +[1] 10 10 7 10 9 + +> languages <- c("Fortran", "R", "Python", "Julia") +> sample(languages, 1) +[1] "Python" + +> sample(c(TRUE, FALSE), 5, replace = TRUE) # coin flips +[1] FALSE TRUE FALSE TRUE TRUE +``` + +## Floating-point numbers + +For a random-uniform distribution use `runif()`. +Both `min` and `max` can be specified but default to 0 and 1. + +```R +> runif(5) +[1] 0.3038506 0.3527959 0.3319309 0.4846354 0.4386279 + +> runif(5, max = 100) +[1] 79.70762 51.62232 52.85281 71.08571 63.94380 +``` diff --git a/concepts/randomness/links.json b/concepts/randomness/links.json new file mode 100644 index 00000000..013f4e6e --- /dev/null +++ b/concepts/randomness/links.json @@ -0,0 +1,14 @@ +[ + { + "url": "http://www.cookbook-r.com/Numbers/Generating_random_numbers/", + "description": "Cookbook for R" + }, + { + "url": "https://www.educba.com/random-number-generator-in-r/", + "description": "EDUCBA" + }, + { + "url": "https://www.statology.org/r-random-number/", + "description": "Statology" + } +] diff --git a/config.json b/config.json index 01ec79ac..bd127020 100644 --- a/config.json +++ b/config.json @@ -569,6 +569,11 @@ "uuid": "2751b6f2-7d71-4397-b063-9bf927a57756", "slug": "booleans", "name": "Booleans" + }, + { + "uuid": "9de56486-6c39-4c08-9d5a-db78a524b0b1", + "slug": "randomness", + "name": "Randomness" } ], "key_features": [