- C
- Clojure
- C++
- C#
- C#
- D
- Dart
- Dart
- Elixir
- Erlang
- Fortran
- Go
- Go
- Go
- Go
- Go
- Groovy
- Haskell
- JS
- Java
- Java
- Kotlin
- Kotlin
- Lisp
- Lua
- Lua
- Obj-C
- PHP
- Pascal
- Perl
- Python
- Python
- Ruby
- Rust
- Rust
- Rust
- Scala
- Smalltalk
- VB
std::random_shuffle(x.begin(), x.end());
Already implemented in the standard library (algorithm)
This version uses the built-in random generator
This version uses the built-in random generator
private static Random rng = new Random();
public static void Shuffle<T>(this IList<T> x)
{
int n = x.Count;
while (n > 1) {
n--;
int k = rng.Next(n + 1);
T value = x[k];
x[k] = x[n];
x[n] = value;
}
}
There is no built-in method to support the randomization of a List. This creates an extension method that adds this functionality to any provided List. rng should be declared as a global variable so that only one Random object is created.
See the original attribution URL for better explanation.
See the original attribution URL for better explanation.
module M_shuffle
contains
function scramble( number_of_values ) result(array)
integer,intent(in) :: number_of_values
integer,allocatable :: array(:)
array=[(i,i=1,number_of_values)]
n=1; m=number_of_values
do k=1,2
do i=1,m
call random_number(u)
j = n + FLOOR((m+1-n)*u)
itemp=array(j); array(j)=array(i); array(i)=itemp
enddo
enddo
end function scramble
end module M_shuffle
:
use M_shuffle, only : scramble
newlist=list(scramble(size(list))
The SCRAMBLE function returns an integer array of the size specified
populated with the numbers 1 to "number_of_values" in random order.
A simple way to randomly scramble a list of any type is to create a
random permutation of all the index values of the array and then access
the original list elements using that list of indices. The list itself
can be re-ordered very succintly using array syntax.
populated with the numbers 1 to "number_of_values" in random order.
A simple way to randomly scramble a list of any type is to create a
random permutation of all the index values of the array and then access
the original list elements using that list of indices. The list itself
can be re-ordered very succintly using array syntax.
for (var i = x.length - 1; i > 0; i--) {
var j = Math.floor(Math.random() * (i + 1));
var temp = x[j];
x[j] = x[i];
x[i] = temp;
}
Shuffle a list in-place using the Fisher-Yates algorithm.
Collections.shuffle(x);
Already implemented in standard library.
This method alters the list.
If you need predictable results, see shuffle(List<?>, Random).
This method alters the list.
If you need predictable results, see shuffle(List<?>, Random).
shuffle(asList(x));
Where `x` is an array of Object, and not primitive, data-types.
(let ((end (length x)))
(loop for i from 0 below end
do (rotatef (aref x i)
(aref x (+ i (random (- end i)))))))
This sorts a lisp vector.
pub fn shuffle<T>(vec: &mut [T]) {
let n = vec.len();
for i in 0..(n - 1) {
let j = rand() % (n - i) + i;
vec.swap(i, j);
}
}
pub fn rand() -> usize {
RandomState::new().build_hasher().finish() as usize
}
Generate random index j, such that: i <= j < n
programming-idioms.org