Brian's Man Cave Posted July 23, 2021 Share Posted July 23, 2021 Is there a way to shuffle a group of numbers randomly. I remember in a different programming language I was able to create an array of say 5 numbers. Then grab at random each number to create a new array of the same 5 numbers but in a different order. Or is there an easier method? Quote Link to comment Share on other sites More sharing options...
Arnauld Posted July 23, 2021 Share Posted July 23, 2021 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] 4 Quote Link to comment Share on other sites More sharing options...
+nanochess Posted July 23, 2021 Share Posted July 23, 2021 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 2 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.