Jump to content
IGNORED

Shuffle numbers


Recommended Posts

A popular and efficient shuffle algorithm is Fisher-Yates. It runs in linear time. (I'm using it for instance in Defender of the Crown to shuffle the lords at the beginning of the game.)

 

Quoting the pseudo-code from Wikipedia:

 

-- To shuffle an array a of n elements (indices 0..n-1):
for i from n−1 downto 1 do
     j ← random integer such that 0 ≤ j ≤ i
     exchange a[j] and a[i]

 

  • Like 4
Link to comment
Share on other sites

The easiest method would  be having your list of numbers in an array. And then create the new array.

 

A recommended way to get reasonable random numbers is to have a title screen, because the random number generator is counting on each video frame, and once the user press start it will be a "random" time. So if you run this piece of code in emulation (or even in a real Intellivision), it will generate the same numbers every time unless you add a title screen.

 

DIM random_array(5)

 

CLS

 

' Needs to set array to zero so it can find free slots

FOR c = 0 TO 4

    random_array(c) = 0

NEXT c

 

filling = 0

DO

    number = my_favorite_numbers(filling)

    DO       ' Find a random non-used spot

        c = RANDOM(5)

    LOOP WHILE random_array(c) <> 0

    random_array(c) = number    ' Fill spot

    PRINT AT c * 20 COLOR 6,<>number    ' Debugging

    filling = filling + 1

LOOP WHILE filling < 5

 

WHILE 1: WEND     ' So we don't make CPU to run over the data

 

my_favorite_numbers: DATA 4, 12, 20, 15, 3

 

 

  • Like 2
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...