Numpy shuffle multidimensional array by row only, keep column order unchanged

You can use numpy.random.shuffle(). This function only shuffles the array along the first axis of a multi-dimensional array. The order of sub-arrays is changed but their contents remains the same. In [2]: import numpy as np In [3]: In [3]: X = np.random.random((6, 2)) In [4]: X Out[4]: array([[0.71935047, 0.25796155], [0.4621708 , 0.55140423], [0.22605866, 0.61581771], … Read more

What, if anything, is wrong with this shuffling algorithm and how can I know?

General remark My personal approach about correctness of probability-using algorithms: if you know how to prove it’s correct, then it’s probably correct; if you don’t, it’s certainly wrong. Said differently, it’s generally hopeless to try to analyse every algorithm you could come up with: you have to keep looking for an algorithm until you find … Read more

How to randomize a vector

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’).

shuffle vs permute numpy

np.random.permutation has two differences from np.random.shuffle: if passed an array, it will return a shuffled copy of the array; np.random.shuffle shuffles the array inplace if passed an integer, it will return a shuffled range i.e. np.random.shuffle(np.arange(n)) If x is an integer, randomly permute np.arange(x). If x is an array, make a copy and shuffle the … Read more

How to shuffle a std::vector?

From C++11 onwards, you should prefer: #include <algorithm> #include <random> auto rng = std::default_random_engine {}; std::shuffle(std::begin(cards_), std::end(cards_), rng); Live example on Coliru Make sure to reuse the same instance of rng throughout multiple calls to std::shuffle if you intend to generate different permutations every time! Moreover, if you want your program to create different sequences … Read more

Is it correct to use JavaScript Array.sort() method for shuffling?

After Jon has already covered the theory, here’s an implementation: function shuffle(array) { var tmp, current, top = array.length; if(top) while(–top) { current = Math.floor(Math.random() * (top + 1)); tmp = array[current]; array[current] = array[top]; array[top] = tmp; } return array; } The algorithm is O(n), whereas sorting should be O(n log n). Depending on … Read more