Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

randomness concept #274

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions concepts/randomness/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"authors": ["colinleach"],
"contributors": [],
"blurb": "R offers a rich variety of ways to generate random values, for many statistical distributions."
}
39 changes: 39 additions & 0 deletions concepts/randomness/about.md
Original file line number Diff line number Diff line change
@@ -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.
36 changes: 36 additions & 0 deletions concepts/randomness/introduction.md
Original file line number Diff line number Diff line change
@@ -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
```
14 changes: 14 additions & 0 deletions concepts/randomness/links.json
Original file line number Diff line number Diff line change
@@ -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"
}
]
5 changes: 5 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand Down