How to resolve the algorithm Knuth shuffle step by step in the Quackery programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Knuth shuffle step by step in the Quackery programming language

Table of Contents

Problem Statement

The   Knuth shuffle   (a.k.a. the Fisher-Yates shuffle)   is an algorithm for randomly shuffling the elements of an array.

Implement the Knuth shuffle for an integer array (or, if possible, an array of any type).

Given an array items with indices ranging from 0 to last, the algorithm can be defined as follows (pseudo-code):

(These are listed here just for your convenience; no need to demonstrate them on the page.)

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Knuth shuffle step by step in the Quackery programming language

Source code in the quackery programming language

  [ [] swap dup size times
      [ dup size random pluck
        nested rot join swap ]
    drop ]                      is shuffle (     [ --> [ )

  [ temp put
    2dup swap
    temp share swap peek
    temp share rot peek
    dip 
      [ swap
        temp take 
        swap poke 
        temp put ]  
    swap 
    temp take 
    swap poke ]                 is [exch]  ( n n [ --> [ )

  [ dup size 1 - times
      [ i 1+ dup 1+ random
        rot [exch] ] ]         is knuffle (     [ --> [ )

  

You may also check:How to resolve the algorithm OpenGL step by step in the D programming language
You may also check:How to resolve the algorithm Bulls and cows step by step in the Tcl programming language
You may also check:How to resolve the algorithm Copy stdin to stdout step by step in the Haskell programming language
You may also check:How to resolve the algorithm Null object step by step in the PicoLisp programming language
You may also check:How to resolve the algorithm Singly-linked list/Traversal step by step in the Clojure programming language