Your call to `sample` does not specify the `size` or the number of values to return, so it defaults to the same number in `x`, in this case 10. The `matrix` function then repeats the vector of 10 enough times to fill in the matrix.
To do what you want you just need to specify the `size` as the total number of values you want sampled, 50 or 5*10 for your case. So the following should do what you want: matrix(sample(1:10, 5*10, replace=TRUE), 5, 10, byrow=TRUE) In this case the `byrow` does not really matter much since you are just filling in random values. Hope this helps, On Thu, Mar 13, 2025 at 3:23 PM Kevin Zembower via R-help <r-help@r-project.org> wrote: > > Hello, all, > > I'm learning to do randomized distributions in my Stats 101 class*. I > thought I could do it with a call to sample() inside a matrix(), like: > > > matrix(sample(1:10, replace=TRUE), 5, 10, byrow=TRUE) > [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] > [1,] 8 2 3 1 8 2 8 8 9 8 > [2,] 8 2 3 1 8 2 8 8 9 8 > [3,] 8 2 3 1 8 2 8 8 9 8 > [4,] 8 2 3 1 8 2 8 8 9 8 > [5,] 8 2 3 1 8 2 8 8 9 8 > > > > Imagine my surprise to learn that all the rows were the same > permutation. I thought each time sample() was called inside the matrix, > it would generate a different permutation. > > I modeled this after the bootstrap sample techniques in > https://pages.stat.wisc.edu/~larget/stat302/chap3.pdf. I don't > understand why it works in bootstrap samples (with replace=TRUE), but > not in randomized distributions (with replace=FALSE). > > Thanks for any insight you can share with me, and any suggestions for > getting rows in a matrix with different permutations. > > -Kevin > > *No, this isn't a homework problem. We're using Lock5 as the text in > class, along with its StatKey web application. I'm just trying to get > more out of the class by also solving our problems using R, for which > I'm not receiving any class credit. > > ______________________________________________ > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide https://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. -- Gregory (Greg) L. Snow Ph.D. 538...@gmail.com ______________________________________________ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide https://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.