'CgShuffleAlgorithm
'* for highly repetitive arrays and not so repetitive ones
'* this is a SLIGHT modification of Knuth Shuffle
OPTION _EXPLICIT
DIM CgMainDemoLow AS _INTEGER64: CgMainDemoLow = 0
DIM CgMainDemoHigh AS _INTEGER64: CgMainDemoHigh = CgMainDemoLow + 1048575
'* rounds defines the number of attempts to find a non-matching array element
DIM rounds AS INTEGER: rounds = 4
'* minimum array element value
DIM CgMainDemoCSTAMin AS DOUBLE: CgMainDemoCSTAMin = 0
'* maximum array element
DIM CgMainDemoCSTAMax AS DOUBLE: CgMainDemoCSTAMax = 1
REDIM CgShuffleTestAray(CgMainDemoLow TO CgMainDemoHigh) AS DOUBLE
'* lagre enough to handle array index values beyond most practical applications because you never konow.
DIM v AS _INTEGER64
'* generate
FOR v = CgMainDemoLow TO CgMainDemoHigh
CgShuffleTestAray(v) = CgMainDemoCSTAMin + INT(RND * (CgMainDemoCSTAMax - CgMainDemoCSTAMin + 1 / 2))
NEXT
'* display results
FOR v = CgMainDemoLow TO CgMainDemoHigh
PRINT CgShuffleTestAray(v);
NEXT
CgShuffle CgShuffleTestAray(), CgMainDemoLow, CgMainDemoHigh, rounds
SUB CgShuffle (a() AS DOUBLE, start AS _INTEGER64, finish AS _INTEGER64, rounds AS INTEGER)
DIM cgsp AS _INTEGER64: cgsp = start
DIM cgsq AS _INTEGER64: cgsq = finish
DIM cgsr AS _INTEGER64: cgsr = cgsq - cgsp
DIM cgss AS _UNSIGNED _INTEGER64
DIM cgsh AS _INTEGER64
DO WHILE cgsr > 0
cgss = rounds
DO
cgsh = cgsp + INT(RND * cgsr) + 1
IF a(cgsp) <> a(cgsh) THEN
SWAP a(cgsp), a(cgsh)
EXIT DO
END IF
cgss = cgss - 1
LOOP UNTIL cgss < 1
cgsr = cgsr - 1
cgsp = cgsq - cgsr
LOOP
END SUB