import array ; sorted? import math ; pow import string ; strtoa

; seeds the random number generator with integer S ; [S] => [] srand: ^$seed ret

; returns the next number N in the linear congruential generator (better MINSTD) ; [] => [N] rand:

push $seed dup dup load
push 48271 mul
push 2,31 :pow $-- mod
store load ret

; returns a random integer I between A and B (inclusive) ; [A B] => [I] rand_range:

$++ copy 1 sub :rand swap mod add ret

; returns an array A of N random integers between 1 and D (inclusive) ; [N D] => [A] dice: ^-2 dup ^-1 times (push 1 @-2 :rand_range) @-1 ret

; shuffles the array A in-place using the modern Fisher-Yates algorithm ; [A] => [A'] shuffle: dup $– ^-3 _shuffle_loop:

push 0 @-3 :rand_range @-3 :aryswap
push -3 :dec @-3 push -1 mul jn _shuffle_loop ret

; shuffles the characters of the string S, producing a random anagram ; [S] => [S'] strfry: :strtoa :shuffle pop :strpack ret

; sorts the array A if you're lucky ; [A] => [A'] bogosort: :shuffle :arydup :sorted? jz bogosort ret