Mercurial > lbo > hg > juliaplay
changeset 18:f099b399e2f0
arrays: Fisher-Yates
author | Lewin Bormann <lbo@spheniscida.de> |
---|---|
date | Tue, 28 Feb 2023 15:17:55 +0100 |
parents | f6833e5295d6 |
children | 5625644a818d |
files | julia/arrays.jl |
diffstat | 1 files changed, 18 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- a/julia/arrays.jl Tue Feb 28 13:43:46 2023 +0100 +++ b/julia/arrays.jl Tue Feb 28 15:17:55 2023 +0100 @@ -352,3 +352,21 @@ end max_encountered end + +"""Fisher-Yates""" +function shuffle(a::AbstractVector) + for i = length(a):-1:1 + ix = rand(UInt) % i + a[i], a[ix+1] = a[ix+1], a[i] + end +end + +function visualizeshuffle(n, m) + base = 1:n + mat = zeros(Int, n, m) + for i = 1:m + mat[:,i] .= collect(base) + shuffle(@view mat[:,i]) + end + mat +end