Replicate a function 100 times

Hi,

I am trying to replicate this simple function 100 times.

sample(1:49,6,replace = F)

but when I try to use replicate to do this 100 times, instead it gives me 100 numbers.

replicate(100,sample(1:49,6,replace = F))

I am just looking for 6 numbers to be generated between 1:49 for 100 times.

thanks!

Hi there,

Here you go.

df <- 
rep(sample(1:49,6, replace = FALSE),times = 100)

length(df)
#> [1] 600


df
#>   [1] 15  4  8 22  7  3 15  4  8 22  7  3 15  4  8 22  7  3 15  4  8 22  7  3 15
#>  [26]  4  8 22  7  3 15  4  8 22  7  3 15  4  8 22  7  3 15  4  8 22  7  3 15  4
#>  [51]  8 22  7  3 15  4  8 22  7  3 15  4  8 22  7  3 15  4  8 22  7  3 15  4  8
#>  [76] 22  7  3 15  4  8 22  7  3 15  4  8 22  7  3 15  4  8 22  7  3 15  4  8 22
#> [101]  7  3 15  4  8 22  7  3 15  4  8 22  7  3 15  4  8 22  7  3 15  4  8 22  7
#> [126]  3 15  4  8 22  7  3 15  4  8 22  7  3 15  4  8 22  7  3 15  4  8 22  7  3
#> [151] 15  4  8 22  7  3 15  4  8 22  7  3 15  4  8 22  7  3 15  4  8 22  7  3 15
#> [176]  4  8 22  7  3 15  4  8 22  7  3 15  4  8 22  7  3 15  4  8 22  7  3 15  4
#> [201]  8 22  7  3 15  4  8 22  7  3 15  4  8 22  7  3 15  4  8 22  7  3 15  4  8
#> [226] 22  7  3 15  4  8 22  7  3 15  4  8 22  7  3 15  4  8 22  7  3 15  4  8 22
#> [251]  7  3 15  4  8 22  7  3 15  4  8 22  7  3 15  4  8 22  7  3 15  4  8 22  7
#> [276]  3 15  4  8 22  7  3 15  4  8 22  7  3 15  4  8 22  7  3 15  4  8 22  7  3
#> [301] 15  4  8 22  7  3 15  4  8 22  7  3 15  4  8 22  7  3 15  4  8 22  7  3 15
#> [326]  4  8 22  7  3 15  4  8 22  7  3 15  4  8 22  7  3 15  4  8 22  7  3 15  4
#> [351]  8 22  7  3 15  4  8 22  7  3 15  4  8 22  7  3 15  4  8 22  7  3 15  4  8
#> [376] 22  7  3 15  4  8 22  7  3 15  4  8 22  7  3 15  4  8 22  7  3 15  4  8 22
#> [401]  7  3 15  4  8 22  7  3 15  4  8 22  7  3 15  4  8 22  7  3 15  4  8 22  7
#> [426]  3 15  4  8 22  7  3 15  4  8 22  7  3 15  4  8 22  7  3 15  4  8 22  7  3
#> [451] 15  4  8 22  7  3 15  4  8 22  7  3 15  4  8 22  7  3 15  4  8 22  7  3 15
#> [476]  4  8 22  7  3 15  4  8 22  7  3 15  4  8 22  7  3 15  4  8 22  7  3 15  4
#> [501]  8 22  7  3 15  4  8 22  7  3 15  4  8 22  7  3 15  4  8 22  7  3 15  4  8
#> [526] 22  7  3 15  4  8 22  7  3 15  4  8 22  7  3 15  4  8 22  7  3 15  4  8 22
#> [551]  7  3 15  4  8 22  7  3 15  4  8 22  7  3 15  4  8 22  7  3 15  4  8 22  7
#> [576]  3 15  4  8 22  7  3 15  4  8 22  7  3 15  4  8 22  7  3 15  4  8 22  7  3

Created on 2022-01-11 by the reprex package (v2.0.0)

Sorry, but it looks like the numbers generated using the solution sent still has a pattern to it.

I'm sorry, but what am I doing wrong with replicate?

A definition I found says *" The replicate() function can be used for creating simulations as it can repeat an expression a specific number of times."

I am actually not sure as I am able to run it on my side as you can see below. Structure is slightly different and potentially easier to use depending on your problem. I am currently running on 4.0.2 here.

df <- 
rep(sample(1:49,6, replace = FALSE),times = 100)

length(df)
#> [1] 600


df
#>   [1] 18  8 30 35 14 22 18  8 30 35 14 22 18  8 30 35 14 22 18  8 30 35 14 22 18
#>  [26]  8 30 35 14 22 18  8 30 35 14 22 18  8 30 35 14 22 18  8 30 35 14 22 18  8
#>  [51] 30 35 14 22 18  8 30 35 14 22 18  8 30 35 14 22 18  8 30 35 14 22 18  8 30
#>  [76] 35 14 22 18  8 30 35 14 22 18  8 30 35 14 22 18  8 30 35 14 22 18  8 30 35
#> [101] 14 22 18  8 30 35 14 22 18  8 30 35 14 22 18  8 30 35 14 22 18  8 30 35 14
#> [126] 22 18  8 30 35 14 22 18  8 30 35 14 22 18  8 30 35 14 22 18  8 30 35 14 22
#> [151] 18  8 30 35 14 22 18  8 30 35 14 22 18  8 30 35 14 22 18  8 30 35 14 22 18
#> [176]  8 30 35 14 22 18  8 30 35 14 22 18  8 30 35 14 22 18  8 30 35 14 22 18  8
#> [201] 30 35 14 22 18  8 30 35 14 22 18  8 30 35 14 22 18  8 30 35 14 22 18  8 30
#> [226] 35 14 22 18  8 30 35 14 22 18  8 30 35 14 22 18  8 30 35 14 22 18  8 30 35
#> [251] 14 22 18  8 30 35 14 22 18  8 30 35 14 22 18  8 30 35 14 22 18  8 30 35 14
#> [276] 22 18  8 30 35 14 22 18  8 30 35 14 22 18  8 30 35 14 22 18  8 30 35 14 22
#> [301] 18  8 30 35 14 22 18  8 30 35 14 22 18  8 30 35 14 22 18  8 30 35 14 22 18
#> [326]  8 30 35 14 22 18  8 30 35 14 22 18  8 30 35 14 22 18  8 30 35 14 22 18  8
#> [351] 30 35 14 22 18  8 30 35 14 22 18  8 30 35 14 22 18  8 30 35 14 22 18  8 30
#> [376] 35 14 22 18  8 30 35 14 22 18  8 30 35 14 22 18  8 30 35 14 22 18  8 30 35
#> [401] 14 22 18  8 30 35 14 22 18  8 30 35 14 22 18  8 30 35 14 22 18  8 30 35 14
#> [426] 22 18  8 30 35 14 22 18  8 30 35 14 22 18  8 30 35 14 22 18  8 30 35 14 22
#> [451] 18  8 30 35 14 22 18  8 30 35 14 22 18  8 30 35 14 22 18  8 30 35 14 22 18
#> [476]  8 30 35 14 22 18  8 30 35 14 22 18  8 30 35 14 22 18  8 30 35 14 22 18  8
#> [501] 30 35 14 22 18  8 30 35 14 22 18  8 30 35 14 22 18  8 30 35 14 22 18  8 30
#> [526] 35 14 22 18  8 30 35 14 22 18  8 30 35 14 22 18  8 30 35 14 22 18  8 30 35
#> [551] 14 22 18  8 30 35 14 22 18  8 30 35 14 22 18  8 30 35 14 22 18  8 30 35 14
#> [576] 22 18  8 30 35 14 22 18  8 30 35 14 22 18  8 30 35 14 22 18  8 30 35 14 22


df2 <- 
replicate(100,sample(1:49,6,replace = FALSE))

df2
#>      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14]
#> [1,]   30   39   45   12   32   48    2   15   14    42    17     6    42     3
#> [2,]   45   42   27   43   38   14    4   43   46    47     9    36     5     2
#> [3,]   33   14   46   49   28   11   48    8    2     4     8     5    25    29
#> [4,]   26   28    2   47    4   28   35   42   30     5    38     7    13    10
#> [5,]   21   23    7   23   22   12   49   17   21     6    10    26    23    36
#> [6,]   41   34   36   10   47   25   39   34   19    24    46    20    18    49
#>      [,15] [,16] [,17] [,18] [,19] [,20] [,21] [,22] [,23] [,24] [,25] [,26]
#> [1,]     7    25    33     9    36    36     3    26    46    32    45    48
#> [2,]    46     1    37     1    30    25    10    13    48    40    40    11
#> [3,]    31    38    16    14    32    39    28    45    47     6    38    34
#> [4,]    21    16     2    10    23    19    43    25    39     7    46    18
#> [5,]    33    22     1     8     6    14    36    30     3    42     1    46
#> [6,]    13    21    30     3    13    17    35    39     7    16    32    42
#>      [,27] [,28] [,29] [,30] [,31] [,32] [,33] [,34] [,35] [,36] [,37] [,38]
#> [1,]    32     2    18    26     6    20     6     6    29    44    21    39
#> [2,]    37    36     3    10    30    30    32    22    34    39     3     7
#> [3,]    38    13    36    30    31    48    26    34     2    29    26    21
#> [4,]    45     4    10    20    46    16    22    37    41    34    49    40
#> [5,]    16    23    49    46    29    36     5    23    26    22    12    15
#> [6,]     3     8    13    23    44     5    42    33    49    30     8    20
#>      [,39] [,40] [,41] [,42] [,43] [,44] [,45] [,46] [,47] [,48] [,49] [,50]
#> [1,]    21    20     6    16    16     8    15    39    18    48    23    46
#> [2,]     3    13     7    18     6    29     1     9    45    14    21    18
#> [3,]    11     3    12    10    13    48    36    11    24    17    36    40
#> [4,]    27    28    30    35    24     2    22     4    11     1    44    10
#> [5,]    42    30    42    15    35    25    27    48    42    33     4    21
#> [6,]    29    43    22    13    31    39    20    42    37    42    46     8
#>      [,51] [,52] [,53] [,54] [,55] [,56] [,57] [,58] [,59] [,60] [,61] [,62]
#> [1,]    18    26    33    18    29     2    17     6     9    15    11    45
#> [2,]    32    20    30    26    42    44    48     8     2    41    46    30
#> [3,]    28     1    46    14    47    45    30    46    23    13     4    34
#> [4,]    48    42    42    39    35    19    35    19    15    14    12    35
#> [5,]    11     6    25    21    46    34    43     9    45    10    10    44
#> [6,]    25    16    14    12     8    32    36     1    36    11    30    20
#>      [,63] [,64] [,65] [,66] [,67] [,68] [,69] [,70] [,71] [,72] [,73] [,74]
#> [1,]    23    48    13    47    33    32    10    33    14    10    36    13
#> [2,]    42    11    30     5     4    31    37    43    46    47    49    38
#> [3,]    26    28    24     8     6     5    49    30     5     6    41    16
#> [4,]    29    15    34    48    39    38     8    40    11    14    17    26
#> [5,]    10    47    23     3    45     1    32    24    21    17    48    14
#> [6,]    22    27    43    34    31    10    21    32     8    15    32     9
#>      [,75] [,76] [,77] [,78] [,79] [,80] [,81] [,82] [,83] [,84] [,85] [,86]
#> [1,]     9     2    37    14    39    10    47    39     6    27    17    17
#> [2,]    12    41    10    34    16    26     8    45    26    19    26     5
#> [3,]    40    20    21    40    12    23    19    30     3    45    28    45
#> [4,]     1    19    49    33    27    27     6    41    12    38    40    22
#> [5,]    37    38    48    17    29    29    10    36    45    30     6    34
#> [6,]    34     5    34     9    28    47    49    11    21    12     3    13
#>      [,87] [,88] [,89] [,90] [,91] [,92] [,93] [,94] [,95] [,96] [,97] [,98]
#> [1,]    26    18    39    45    31     9    36    45    13     8    45    41
#> [2,]    23    37    47    35    41    40    26    25    26    14    48    22
#> [3,]    16    39    31    27    47    26    14    20    31    40    43    34
#> [4,]    12    49     5    49    19    17    23    48    29    22    37    20
#> [5,]    25    16     2    40    29    18    46     1     4    36    36    42
#> [6,]    44     7     1    38    11     5    33    41    28    42    16     4
#>      [,99] [,100]
#> [1,]    27     15
#> [2,]     4     21
#> [3,]    41     10
#> [4,]    43     32
#> [5,]    38     22
#> [6,]    19      3

Created on 2022-01-11 by the reprex package (v2.0.0)

I would personally just have done the below. This way I have 600 numbers taken equally from 1:49 and I can split that into what I need. I can't think it would be problematic to sample with replacement

df <- 
sample(1:49,600, replace = TRUE)

1 Like

hmm.. yes, you are right.

thank you for looking into this!

1 Like

I'm assuming you want 100 simulated lottery draws of 6 from 49.

In that case your first replicate attempt was correct. It resulted in a 100x6 matrix.

If you run this it produces the same output as a list:

replicate(100, sample(1:49, 6, replace = FALSE), simplify = FALSE)
1 Like

Hi there, I hope get it right what you wanted

for (i in 1:100) {
    print(sample(1:49, 6, replace = FALSE))
}
#> [1] 24 30 44 16 27 36
#> [1] 36 16 47  1 19 22
#> [1] 16 39 41  7 35 42
#> [1] 46  8  1 49 24 20
#> [1] 39 46 16  6 44 24
#> [1] 11 17 24 12 37 48
#> [1] 27  2 33 10 19 49
#> [1] 47 29  4 34 41 17
#> [1] 33 16 29 48 42 38
#> [1] 46 36 23 19 33 45
#> [1] 18 33 15  2 36 27
#> [1] 49 23  4 11  1 35
#> [1] 17 39  7 47 22 13
#> [1] 36 22 23 34 40 26
#> [1]  8 36 41 11 35 47
#> [1]  2 11 32 47 20  7
#> [1] 26 10 43 29 33 32
#> [1] 37 16 46 11  6 14
#> [1] 27 36 46  7 38  4
#> [1] 41  1 34 21  8 25
#> [1] 18 31 17 47 45  8
#> [1] 22 36  7 21  4 33
#> [1]  2 16  6 22 11  8
#> [1] 48 41 21 43  9 23
#> [1] 45 27 38 28 44 49
#> [1] 14  8 15 16 17 12
#> [1] 10 34  5 46 35 40
#> [1]  1 19  5  7 22  6
#> [1] 44 36 31 45 43  6
#> [1] 32 29  9 26  4 19
#> [1] 31 10 42 33 19 35
#> [1] 33 24 34 13 43 49
#> [1] 45 14 32 49 27 29
#> [1] 47 31 43  5 21 18
#> [1] 20  6  2  4 41  3
#> [1] 30 23 41 36 13 35
#> [1] 29 45 40  3 16 37
#> [1] 31 12  2 24 49 37
#> [1] 36 47 31 33  9 43
#> [1] 34 41 12  4 35 45
#> [1] 40  6  8 38 21 33
#> [1] 29  4 30 39 25 24
#> [1]  4 18 35 21  9 44
#> [1] 40 39 14  6 26 45
#> [1] 20  2 46 24 30 32
#> [1] 12 46 22 25 20 14
#> [1]  2 15  8 49 45  3
#> [1] 49 14 26 36 20 29
#> [1] 38 25  9 30 17 40
#> [1] 26 30 27 23 42 11
#> [1] 45 18 27 21 38  1
#> [1] 44 34 42 46 14 47
#> [1]  8 36  6 28 46  2
#> [1] 12 36  4 30 11 13
#> [1] 24 10 49  9 16 15
#> [1] 33 11 24 13 40 17
#> [1] 36 43  7 44 23 29
#> [1] 44 28  9 22 36 29
#> [1] 40 15 39 28  1 24
#> [1] 37 39 34 30 40 17
#> [1] 44 40 29  1 21  4
#> [1]  7 33 31 37 45  6
#> [1] 22 19  4 11 42 47
#> [1] 47 18 36 25 41 38
#> [1] 39 37 47 44 28 45
#> [1] 25 33 39 45  9 24
#> [1] 29  5 44  7  3 30
#> [1] 15 35 19 33 27 39
#> [1]  9  5 24 46 31 15
#> [1] 17 27 36 16  2  6
#> [1] 41  4 16 37 15 46
#> [1]  6 48  3 44 17 31
#> [1] 34 22  2 15 12 11
#> [1] 38 13 41  7 48 36
#> [1] 26 22 21 38 39 37
#> [1] 38 22 23 15 12 20
#> [1] 26 32 13  6 40 46
#> [1] 27 15 40  1 33  2
#> [1] 43 12 31  9 45 38
#> [1] 11  1  7 25 49 19
#> [1] 21 14  3  7  2  1
#> [1] 19 10 44 34 42  5
#> [1] 17 22 33 42 29  3
#> [1]  1 42 26 31  5 38
#> [1]  2 16 26  6  3 25
#> [1] 23 38  2 43 28 16
#> [1]  9 13 35 15 19 22
#> [1] 11 36 37  2 44 38
#> [1] 22 39 43 29  5 49
#> [1] 14 29 26 21 22 45
#> [1] 35 24 28 44 36  1
#> [1] 42 48 35 19 18 12
#> [1]  4 15 23 29  8 19
#> [1] 23 48 28 37 30 26
#> [1] 11 27 48 34 22 12
#> [1]  1 22  5 15 10  4
#> [1] 36 12 18  3 22 30
#> [1]  8  5 33 21 18 45
#> [1] 43 41 21 46 31 13
#> [1] 34 46  4 26 47 38
Created on 2022-01-13 by the reprex package (v2.0.1)

rep(sample(1:49,6, replace=FALSE), times = 100) will sample once and repeat the results 100 times, hence the same six values repeated.