I would like to randomly reorganize the order of the numbers in a vector, in a simple one-line command?
My particular vector V has 150 entries for each value from 1 to 10:
V <- rep(1:10, each=150)
How to shuffle a std::vector in C++ A vector shuffle can be done in the Fisher-Yates shuffle algorithm. In this algorithm, a linear scan of a vector is done and then swap each element with a random element among all the remaining element, including the element itself.
The shuffle() function in C++ is a function in vector library. It is a function that will rearrange the elements of any range by placing the elements at random positions. To shuffle it uses a uniform random generator which helps in shuffling the elements.
Yes.
sample(V)
From ?sample
:
For ‘sample’ the default for ‘size’ is the number of items inferred from the first argument, so that ‘sample(x)’ generates a random permutation of the elements of ‘x’ (or ‘1:x’).
Use sample
function
V<-rep(1:10, each=150) set.seed(001) # just to make it reproducible sample(V)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With