# R Project: generate random numbers

Obtaining random numbers is often an essential requirement for scientific research. In this way, we can reduce any bias generated by our person when selecting a sample.

Don’t you know what I mean? Imagine for a moment that we have to select 10 specimens to evaluate a hypothesis. How can referees and/or readers of the scientific paper be sure our selection has been made based on empirical arguments?

Well, one possible solution is to obtain random numbers. And this is very easy to do in R.

# Generate random numbers with the command sample

Let’s see how to get random numbers with the sample command in R. Here is an example:

```> sample(1:30,10,replace=F)
[1] 5 3 19 10 28 4 11 23 16 22```

We will explain the attributes of the sample command:

• 1:30 = data range is between both numbers (including 1 and 30).
• 10 = number of random numbers that we want to get from the previous range.
• replace = F/T = if you want the possibility that random numbers can be repeated (i.e. get for example getting two numbers 3), leave the value T (true). If you don’t want repetitions, write the value F (false).

## Example 1

From 1 to 10, get 4 random numbers with the possibility of repetition:

```> sample(1:10,4,replace=T)
[1] 5 5 4 1```

It is important to highlight that the fact that random numbers can be repeated (replace = T) does not necessarily mean they will always appear.

## Example 2

From 50 to 100, get 10 numbers without the possibility of repetition:

```> sample(50:100,10,replace=F)
[1] 76 88 67 78 95 66 94 62 64 58
```

# One last idea

The repetition of a particular command several times will obviously give a different random number selection. See, for example, this command running six times:

```> sample(1:50,10,replace=F)
[1] 12 39 4 29 5 3 14 1 25 40
> sample(1:50,10,replace=F)
[1]  6 32 28 27 47 40 22 17 36 14
> sample(1:50,10,replace=F)
[1] 14 23 47 13 44 45 36 11 49 34
> sample(1:50,10,replace=F)
[1] 19 7 39 41 18 5 42 2 45 34
> sample(1:50,10,replace=F)
[1]  8 35 6 34 12 25 47 24 28 39
> sample(1:50,10,replace=F)
[1] 24 27 31 13 48 12 26 42 2 21```

Long live Open-Source Software!