Playing around, checking it out...
Fisher-Yates shuffle for containers.Array( of T ).
Generated by DeepSeek. Need to include documentation links in prompt.
'' RandomSort.ti -- generic Fisher-Yates shuffle for containers.Array( of T )
''
'' The file that includes this one must import std/containers first:
''
'' #import "std/containers"
'' #include once "RandomSort.ti"
'' using containers
''
'' dim as Array( of long ) a = { 1, 2, 3, 4, 5 }
'' RandomSort( a )
''
'' RandomSort takes an EXISTING array by reference and shuffles it IN PLACE:
'' it rearranges the array you pass, allocates no new array, and returns
'' nothing -- the same convention as Sort( a ). An empty array or a one-element
'' array is left alone.
''
'' Elements are moved with SWAP, so no T is copied and the shuffle needs neither
'' '=' nor '<' on T -- any element type works.
''
'' RANDOMNESS AND THE SEED
''
'' RandomSort( a ) draws from the global RND stream. Call RANDOMIZE once before
'' the shuffle to pick the seed:
''
'' randomize '' time-derived: a different order each run
'' randomize 12345 '' fixed: the same order every run
''
'' If RANDOMIZE is never called, RND starts from its default seed, so every run
'' of the program produces the same shuffle. Call RANDOMIZE again whenever you
'' want to start a fresh sequence.
''
'' Algorithm: Fisher-Yates, descending. i runs from Count( )-1 down to 1, j is
'' drawn from 0..i INCLUSIVE, and a[ i ] and a[ j ] are swapped. That is the
'' form under which every permutation is equally likely; an ascending loop, or
'' an exclusive 0..i-1 bound, is the subtly wrong variant to avoid.
#pragma once
using containers
'' Shuffle an existing Array( of T ) in place.
public sub RandomSort( of T )( byref arr as Array( of T ) )
dim as long i = arr.Count( ) - 1
while( i > 0 )
dim as long j = int( rnd * ( i + 1 ) )
swap arr.Data( )[ i ], arr.Data( )[ j ]
i -= 1
wend
end sub
'' RandomSort.tiko -- sample for the RandomSort.ti include file.
''
'' tiko RandomSort.tiko
''
'' RandomSort( a ) takes an EXISTING containers.Array and shuffles that array in
'' place -- nothing is returned. Each sample below builds the array first, then
'' passes it in.
#import "std/containers"
using containers
#include once "RandomSort.ti"
sub Show( byref a as Array( of long ) )
for i as long = 0 to a.Count( ) - 1
print a[ i ];
next
print
end sub
function Deck( byval n as long ) as Array( of long )
dim as Array( of long ) d
for i as long = 1 to n
d.Push( i )
next
return d
end function
'' A fixed seed makes this output identical on every run.
randomize 12345
'' An existing array, built by Deck( ), is passed to RandomSort.
dim as Array( of long ) cards = Deck( 20 )
print "before: ";
Show( cards )
RandomSort( cards )
print "after: ";
Show( cards )
'' Passing the same existing array again reshuffles it in place.
RandomSort( cards )
print "reshuffled: ";
Show( cards )
'' A shuffle is a permutation: sorting a copy must restore 1..20 exactly.
dim as Array( of long ) check = cards
Sort( check )
dim as boolean permutation = ( check.Count( ) = 20 )
for i as long = 0 to check.Count( ) - 1
if check[ i ] <> i + 1 then permutation = false
next
print "is a permutation: "; permutation
'' Any element type works -- no '=' or '<' is required on T. This existing
'' array of names is shuffled the same way.
dim as Array( of ustring ) names
names.Push( "Ada" ) : names.Push( "Grace" ) : names.Push( "Alan" )
names.Push( "Edsger" ) : names.Push( "Barbara" )
RandomSort( names )
print "names: ";
for i as long = 0 to names.Count( ) - 1
print names[ i ]; " ";
next
print
'' For a fresh order on each run, replace the fixed seed above with a plain
'' RANDOMIZE, which seeds from the clock.
Cool stuff Stan 8)
I'm thinking that it would be a descent idea to add SHUFFLE and CHOICE to Containers. Sounds like it could be useful.
CHOICE would basically allow you to pick something randomly from the Container.
dim r as Rng = RngFromEntropy( )
print Choice( r, names ) ' print a random name from the "names" array
Choice( r, scores ) += 10 ' update a random score from the "scores" array
@Stan Duraham I have implemented your shuffle and choice suggestions into the lates 0.1.4 release.
Also, there is now a fully compliant AI Cheatsheet available if you wish to feed it into your LLVM's when developing with Tiko. It is linked from several places in the Github, wiki, website, but here is the raw markdown version link:
https://github.com/PaulSquires/tiko-lang/blob/master/wiki/AI-Cheatsheet.md