shuffle!

..  shuffle!([rng,] v)

In-place version of :func:`shuffle`.

Examples

The shuffle! function in Julia is an in-place version of the shuffle function. It shuffles the elements of an array or collection randomly, modifying it in-place. Here are some examples of how to use the shuffle! function:

  1. Shuffle an array of integers:

    julia> arr = [1, 2, 3, 4, 5];
    julia> shuffle!(arr)
    5-element Array{Int64,1}:
    5
    2
    4
    1
    3

    This example shuffles the elements of the array arr randomly.

  2. Shuffle a string:
    julia> str = "hello";
    julia> shuffle!(str)
    "olhle"

    It shuffles the characters of the string str randomly.

Common mistake example:

julia> matrix = [1 2 3; 4 5 6; 7 8 9];
julia> shuffle!(matrix)
ERROR: MethodError: no method matching shuffle!(::Array{Int64,2})

In this example, the shuffle! function is called on a 2-dimensional array (matrix), which is not supported. The shuffle! function works only with one-dimensional arrays or collections. Make sure to pass a valid one-dimensional array to the shuffle! function.

See Also

User Contributed Notes

Add a Note

The format of note supported is markdown, use triple backtick to start and end a code block.

*Required Field
Details

Checking you are not a robot: