Liberty BASIC Community Forum
« [RC] Sattolo Cycle »

Welcome Guest. Please Login or Register.
Sep 20th, 2017, 10:29pm


Rules|Home|Help|Search|Recent Posts|Notification


« Previous Topic | Next Topic »
Pages: 1  Notify Send Topic Print
 thread  Author  Topic: [RC] Sattolo Cycle  (Read 131 times)
tenochtitlanuk
Moderator
ImageImageImageImageImage


member is offline

Avatar




Homepage PM

Gender: Male
Posts: 1162
xx [RC] Sattolo Cycle
« Thread started on: Feb 6th, 2017, 12:00pm »

This an easy and rather interesting one- permuting terms in an array so no one is left in its original place.
Sattolo Cycle
Quote:
The Sattolo cycle is an algorithm for randomly shuffling an array in such a way that each element ends up in a new position.

Think of it as shuffling all your dinner-party guests so they ALL have to move to a new place.
I use here a string array so would need to count how many terms to use and redim to be more elegant. I'd actually prefer to save the data in a csv string and use string manipulations. Anyone with a neat alternative or comments?
Code:
data "s", "a", "t", "T", "o", "l", "O", "c", "y", "C", "L", "e"

dim array$( 100)      '   integer array- up to 101 terms

for i =0 to 11
    read n$
    array$( i)   =n$
next i

call showArray

for i = 11 to 1 step -1
    j = int( i *rnd( 1))
    call interchange i, j    '   swap items[i] with items[j]
next i

call showArray

end

sub interchange i, j
    temp$       =array$( i)
    array$( i)  =array$( j)
    array$( j)  =temp$
end sub

sub showArray
    for i =0 to 11
        print " "; array$( i);
    next i
    print ""
end sub
 


Typical output ( you are almost certain to get something different- LB's PRNG isn't THAT bad!
Quote:
s a t T o l O c y C L e
C s l o L a e T t O y c
User IP Logged

Pages: 1  Notify Send Topic Print
« Previous Topic | Next Topic »

Rules|Home|Help|Search|Recent Posts|Notification

Donate $6.99 for 50,000 Ad-Free Pageviews!

| |

This forum powered for FREE by Conforums ©
Sign up for your own Free Message Board today!
Terms of Service | Privacy Policy | Conforums Support | Parental Controls