Paste: shuffle an array
Author: | Pepijn de Vos |
Mode: | forth |
Date: | Mon, 31 Oct 2011 15:29:23 |
Plain Text |
\ Simple random number generator
\ from Leo Brodie, _Starting Forth_
VARIABLE RND \ Holds current result
\ Generate a random integer
: RANDOM RND @ 31421 * 6927 + DUP RND ! ;
\ Return a random integer between 0 and u-1
: CHOOSE
over -
RANDOM swap mod
+ ;
\ Initialize
: RANDOMIZE TIME&DATE + + + + + RND ! ;
\ my code
randomize
: range 0 do i , loop ;
: th cells + ;
: s! swap ! ;
: inspect 0 do dup i cells + @ . loop ;
52 constant #cards
create deck #cards range
variable passing
: copy @ s! ;
: interchange
passing
3 0 do
rot 2dup copy
loop
drop drop drop ;
: shuffle
#cards 0 do
deck i #cards choose th
deck i th
interchange
loop ;
New Annotation