Shuffling An Array

Anything Sinclair ZX Basic related; history, development, tips - differences between BASIC on the ZX80 and ZX81
User avatar
XavSnap
Posts: 1941
Joined: Sat May 10, 2008 4:23 pm
Location: 'Zx81 France' Fb group.

Re: Shuffling An Array

Post by XavSnap »

Hi MrVertigo,

The lower case was dedicated to the inverted characters on the GP100 printer/Centronics & RS232 printers.
Xavier ...on the Facebook groupe : "Zx81 France"(fr)
MrVertigo
Posts: 106
Joined: Fri May 27, 2022 9:06 pm

Re: Shuffling An Array

Post by MrVertigo »

Do you know the maths to work a shuffle array out this way:

1) Decide how many elements are in your array. (Let’s say 5)

2) Work out how many permutations there are for that number of elements. (Simply n!: n as a factorial). In the case of five elements that is 120 possible permutations.

3) Choose a random number between 1 and n! (In this case between 1 and 120. Say we get a random number 79)

4) Get the computer to calculate the 79th permutation. This is the part I don’t understand the maths for, but it involves using the Factorial Number System as a base:

1, 2, 6, 24, 120, 720...etc

Here is a link if you understand it better than me:

https://medium.com/@aiswaryamathur/find ... 81e34ab0c8
MrVertigo
Posts: 106
Joined: Fri May 27, 2022 9:06 pm

Re: Shuffling An Array

Post by MrVertigo »

Oh, I do understand the maths for this now. It’s quite easy once you get your head round the factorial number system as a base. I’ll try and write a routine for it and see if it’s simple or over complicated.
User avatar
XavSnap
Posts: 1941
Joined: Sat May 10, 2008 4:23 pm
Location: 'Zx81 France' Fb group.

Re: Shuffling An Array

Post by XavSnap »

Hi MrVertigo,

The main problem isn't the Maths process, but the ZX BASIC and it will be poor/slow & inaccurate calculate...
The shortest/simpler & clever program is always the faster routine.
:oops:

I really like the ZX81 because of it's as bad as me at math !
Xavier ...on the Facebook groupe : "Zx81 France"(fr)
MrVertigo
Posts: 106
Joined: Fri May 27, 2022 9:06 pm

Re: Shuffling An Array

Post by MrVertigo »

:D
User avatar
XavSnap
Posts: 1941
Joined: Sat May 10, 2008 4:23 pm
Location: 'Zx81 France' Fb group.

Re: Shuffling An Array

Post by XavSnap »

:lol: :lol: :lol:
Xavier ...on the Facebook groupe : "Zx81 France"(fr)
MrVertigo
Posts: 106
Joined: Fri May 27, 2022 9:06 pm

Re: Shuffling An Array

Post by MrVertigo »

I wrote a program in ZX81 BASIC to convert a decimal number to a Factoradic number. This would be the engine to drive a program that would give you the nth permutation of a string.

Can this part of the program be simplified or improved?

Code: Select all

   
    10 LET C=1
    20 LET X=1
    30 LET F=0
    40 PRINT "INPUT DECIMAL"
    50 INPUT A
    60  REM
    70  REM
   100 LET X=X+1
   110 LET C=C*X
   120 IF C<A THEN GOTO 100
   125  REM
   126  REM
   127 IF C=A THEN LET F=1
   130 IF C=A THEN GOTO 1000
   140 LET C=C/X
   150 IF A>=C THEN LET F=F+1
   160 IF A>=C THEN LET A=A-C
   170 IF A=0 THEN GOTO 1000
   180 IF A>=C THEN GOTO 150
   190  REM
   200  REM
   300 PRINT F
   310 LET F=0
   320 LET X=X-1
   330 GOTO 140
  1000 PRINT F
  1005 IF A<>C THEN LET X=X-1
  1010 FOR N=1 TO X
  1020 PRINT 0
  1030 NEXT N
I just noticed I left those REM lines in. They were just so’s I could see the individual parts of the program when I was writing it.
Spinnetti
Posts: 254
Joined: Sat Sep 12, 2020 11:29 pm

Re: Shuffling An Array

Post by Spinnetti »

You guys probably already explored this and I just don't understand, but why not just create a an array the length of your string array with random numbers in the set of integers from 1-n then just use that as an index into your string array? Wouldn't that be pretty quick?
Zeddy: ZX80, ZX81/ZXpand, TS1000/ZXpand, TS1500/Zxpand+,Printer
Speccy: 48k, +, +2, +3, TS2068, "Bare Metal" Pi, Next KS2, IF1/Microdrives/Vdrive/Light Gun/VGA-Joy
QL: Minerva/QL-VGA/Custom PSU
C5: 24v, LiFE battery, Disc brakes
MrVertigo
Posts: 106
Joined: Fri May 27, 2022 9:06 pm

Re: Shuffling An Array

Post by MrVertigo »

The random numbers need to be constrained so’s there are no repeats. Using the method you suggest an element in the string array could be called any number of times, rather than just once. So if you were looking to shuffle a string array with elements 1,2,3,4,5 you could end up with a shuffle like 2,2,1,3,1 which is no good.

Unless I’m misunderstanding what you’re saying.
User avatar
thewiz
Posts: 58
Joined: Sun Aug 16, 2009 8:36 pm
Location: Crewe

Re: Shuffling An Array

Post by thewiz »

Depending on how good rnd is, you could loop thro each element and swap it with a random element. Sometimes an element might get swapped with itself and sometimes an element can get swapped multiple times but not sure if this would be a problem.
Memotech rules
Post Reply