Author Topic: Explain this!  (Read 40 times)

OlDosLover

  • Hero Member
  • *****
  • Posts: 3845
  • OlDosLover
    • Email
Explain this!
« on: April 21, 2013, 01:24:39 AM »
Hi all,
    In this program i am generating the numbers 1 to 64 and then trying to shuffle those numbers like a deck of cards. For some reason it often shows duplicate numbers and missing numbers but at the end test stage it shows only one of each number number is generated. Its got me stumped!
Code: [Select]
REM
DEFINT A-Z

DIM SHARED MixedUp(1 TO 64) AS INTEGER
DIM SHARED Counter(1 TO 64) AS INTEGER

FOR t = 1 TO 64
  MixedUp(t) = t
  PRINT MixedUp(t),
NEXT t

SLEEP
CLS
'RANDOMIZE TIMER
FOR t = 1 TO 64
  z = INT(RND * 64) + 1
  'SWAP MixedUp(z), MixedUp(t)
  Temp1 = MixedUp(t)
  Temp2 = MixedUp(z)
  MixedUp(z) = Temp1
  MixedUp(t) = Temp2
  PRINT MixedUp(t),
  'PRINT z,
NEXT
SLEEP
CLS

FOR t = 1 TO 64
  z = MixedUp(t)
  Counter(z) = Counter(z) + 1
NEXT t
FOR t = 1 TO 64
  PRINT Counter(t),
NEXT t
SLEEP
SYSTEM
OlDosLover.

Mrwhy

  • Hero Member
  • *****
  • Posts: 2882
  • My Dad called me Mr Why when I was 5.
    • Email
Re: Explain this!
« Reply #1 on: April 21, 2013, 01:32:17 AM »
The most common "error" in progs like this is to test an object against itself instead of ignoring that identity each time it arises.

OlDosLover

  • Hero Member
  • *****
  • Posts: 3845
  • OlDosLover
    • Email
Re: Explain this!
« Reply #2 on: April 21, 2013, 01:34:51 AM »
Hi all,
    Ahh i figured it out. Im printing the new shuffled index and it shows a duplicated number. The solution was to wait till the loop is finished and then to print the shuffled numbers and then they show correctly with no duplicates. A silly thing i over looked!
OlDosLover.