-
Notifications
You must be signed in to change notification settings - Fork 75
/
random-numbers-generators.qmd
37 lines (22 loc) · 2.01 KB
/
random-numbers-generators.qmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# Random number generators
R contains several functions to generate random numbers.
Type `?`*`function`* in your console to get information on the function's arguments (i.e. the values that must be provided to obtain the function's result).
The function `sample(x, n, replace = FALSE)` draws `n` values from a given vector `x` without replacement (by default).
Sampling without replacement means that when you repeatedly draw e.g. one item at a time from a pool of items, any item selected during the first draw is not available for selection during the second draw, and the first and second selected items are not in the pool to select from during the third draw, etc. Sampling with replacement means that all the original options are available at each draw.
***
**YOUR TURN:**
Sample 100 values between 3 and 103 with replacement. For this, open the R script(s) with the exercises (`./exercise_script_with_solutions.R` and/or `./exercise_script_without_solutions.R`) from the root of your local repository, review the examples if needed, complete the exercise, and check out the proposed answer.
***
The following functions draw `n` values from distributions with the specified parameters:
* `runif(n, min, max)` draws `n` values from a *uniform* distribution with the specified `min` and `max`.
* `rpois(n, lambda)` draws `n` values from a *Poisson* distribution with the specified `lambda`.
* `rnorm(n, mean, sd)` draws `n` values from a *normal* distribution with the specified `mean` and standard deviation `sd`.
* `rbinom(n, prob)` draws `n` values from a *binomial* distribution with the specified probability `prob`.
***
**YOUR TURN:**
1. Draw 100 values from a normal distribution with a mean of 0 and a standard deviation of 1.
2. Draw 50 values from a normal distribution with a mean of 10 and a standard deviation of 5.
3. Draw 1000 values from a Poisson distribution with a lambda of 50.
4. Draw 30 values from a uniform distribution between 0 and 10.
Try it out in your local exercise script.
***