Jump to content
IGNORED

Shuffle routine not working.


Recommended Posts

I'm trying to create a Basic XL shuffle routine using the Fisher-Yates algorithm found here. For some reason I can't seem to understand, the result of the shuffle always puts the last card (51) in the first position. All the other cards seem to shuffle randomly. Maybe I have just been looking at this too long and am missing something obvious.

 

I'd appreciate anyone looking at this and pointing out my mistake. I'm using the BXL Random function which returns a number between the first and second parameter (inclusive) as it closely approximates the C rand() function which returns a number between 0 and 32767 (inclusive). BXL throws an out of bounds error if I use 0 and 32767, but works OK if I use 0..32766 or 1..32767.

 

The value of K does not appear to be the same each time the code is run and yet the value in the first position of of the Deck array is always 51 after the shuffle.

 

Here is the Basic XL code:

100 Dim Deck(52),Cards(5,5)
105 Rem INITIALIZE THE ARRAY
110 For X=0 To 51:Deck(X)=X:Next X
115 Rem SHUFFLE THE ARRAY
120 For I=0 To 51
130   J=(52-I):K=Random(1,32767)
135   Rem R IS K MOD J
140   R=K-(Int(K/J)*J)
145   Rem SWAP CARDS
150   L=Deck(I):Deck(I)=Deck(R):Deck(R
)=L
155   Print R,
160 Next I
165 Print :Print
170 For I=0 To 51
180   Print Deck(I);"  ";
190 Next I

The code above is being run in Altirra 2.90 using the BXL cart and extensions disk loaded up on an NTSC XE with 320K Rambo enabled for no particular reason. Ignore the Cards Matrix as that is unused (meant to be the 5 X 5 grid of cards arranged on screen in my Monaco Solitare game).

 

Thanks for any comments.

Edited by Geister
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...