Ricky Spanish Posted May 7, 2022 Share Posted May 7, 2022 Was wondering with this small program on how to prevent duplicate numbers. I use to know it many moons ago, can't figure it out now ? 200 Dim Random(49) 210 ? "<clear>" 220 ? "CALCULATING RANDOM NUMBERS" 230 For Delay=1 To 500:Next Delay 240 For Count=1 To 6 250 Random=Int(49*Rnd(0))+1 260 ? Random 270 Next Count Thanks ! Quote Link to comment Share on other sites More sharing options...
TGB1718 Posted May 7, 2022 Share Posted May 7, 2022 Something like this:- You have to store the numbers in the array, then for each new random number, check if it's already in there if it is, just generate a new random number, I've limited the number 1 to 10 in an array of 10, this always generates numbers 1 through 10 showing no duplicates. One warning, don't user variables with the same name even if one is an array and the other an integer i.e. random(49) and random, you will find odd numbers in the array, I think it gets confused. 200 Dim Myarray(10) 210 ? "<CLEAR>" 220 ? "CLCULATING RANDOM NUMBERS" 230 For Delay=1 To 500:Next Delay 240 For Count=1 To 10 250 Mynum=Int(10*Rnd(0))+1 260 For J=1 To Count 270 If Myarray(J)=Mynum Then Pop :Goto 250 : REM Have to POP the J loop 280 Next J 290 Myarray(Count)=Mynum 300 Next Count 310 For Count=1 To 10 320 ? Myarray(Count) 330 Next Count 1 Quote Link to comment Share on other sites More sharing options...
EddyFree Posted May 7, 2022 Share Posted May 7, 2022 I'd use a different technique. I've never worked with Basic XL so I won't give code for it but I'll offer the overall idea instead. Make an array of the values and then just swap two "random positions" in the array for a set number of times in a loop. Here's a code segment in FreeBasic(PC): --------- ' swaps random elements of an array to provide a unique randomized set(no dups) Dim Shared ARRAY(0 to 99) as Integer, x as Integer For x=0 To 99 ARRAY(x)=x Next x For x=1 To 1024 swap ARRAY(Int(Rnd*100)),ARRAY(Int(Rnd*100)) Next x --------- 2 Quote Link to comment Share on other sites More sharing options...
Rybags Posted May 8, 2022 Share Posted May 8, 2022 (edited) A string would be more efficient to track duplicates. If you had a fairly long range then a bitmap would be better but more processing required. If you wanted a really long range then a LFSR simulation might be best. Pokey random numbers + poly noise are done by LFSR. This generates a known sequence that repeats when <number of bits> ^2 -1 combinations have been through. e.g. if you wanted a range of 60,000 you'd use a 16 bit LFSR - it gives numbers 1-65535, you could just throw out ones that are too big. To make it more "random" you could choose a random seed value. Edited May 8, 2022 by Rybags Quote Link to comment Share on other sites More sharing options...
Ricky Spanish Posted May 8, 2022 Author Share Posted May 8, 2022 5 hours ago, TGB1718 said: Something like this:- Worked like a charm. Thanks ! Quote Link to comment Share on other sites More sharing options...
+MrFish Posted May 8, 2022 Share Posted May 8, 2022 Here's the method I came up with (when I was working on my Black Jack program) to shuffle an array representing card values. It randomly selects an index value until there are none left to select, and swaps the value at that index position with the value in the current index position of the loop (if necessary). This takes about 2 seconds in the TBXL interpreter. The only variability is how many times it needs to make a swap, which will never exceed the number of elements -1. As for seeding... I haven't dealt with that part in the program yet, but user button presses from various title screens, or whatever, can be used to get random numbers for creating small timing loops to use in between card selections during the shuffle and/or to determine how many times the deck would get reshuffled; that coupled randomness of time it takes to play through a deck (or shoe, as it will eventually be) -- when the reshuffle happens during play -- would be sufficient in this case. 1 Quote Link to comment Share on other sites More sharing options...
Ricky Spanish Posted May 8, 2022 Author Share Posted May 8, 2022 @MrFish I like really like your AtariVerse site. Lot's of interesting things on it ! Quote Link to comment Share on other sites More sharing options...
+MrFish Posted May 8, 2022 Share Posted May 8, 2022 On 5/8/2022 at 1:01 AM, MrFish said: Here's the method I came up with (when I was working on my Black Jack program) to shuffle an array representing card values. I threw together a quick program to allow testing the method. The program lets you select a size, and choose whether you want to shuffle a freshly ordered array or reshuffle. One note on the code posted above (and the program attached here) is that the comparison can just be between the two indices if you're using unique elements (which both examples are). The reason I coded it like above, though, is in preparation for using a shoe of cards, which will have duplicate values. Therefore, it will potentially save a lot of swaps. With unique values, there's no necessity to compare the values (they're all different) and will save some small amount of processing if you eliminate it. The disk autoboots the program. Shuffle Deck.atr On 5/8/2022 at 4:19 PM, Ricky Spanish said: @MrFish I like really like your AtariVerse site. Lot's of interesting things on it ! Thanks... it's growing; but I've no time for it right now. 2 Quote Link to comment Share on other sites More sharing options...
+MrFish Posted May 11, 2022 Share Posted May 11, 2022 On 5/8/2022 at 6:52 PM, MrFish said: One note on the code posted above (and the program attached here) is that the comparison can just be between the two indices if you're using unique elements (which both examples are). The reason I coded it like above, though, is in preparation of using a shoe of cards, which will have duplicate values. Therefore, it will potentially save a lot of swaps. With unique values, there's no necessity to compare the values (they're all different) and will save some small amount of processing if you eliminate it. Another note here... arrays are expensive in terms of memory. I used an array because this is just part of some preliminary coding for the project. Strings are a lot cheaper. All my values are going to be less than 255 too. So, it's quite simple to do with string memory offsets. I'd never really thought of it before, but values up to 65,535 are easily possible using strings too. Just store and retrieve your values using DPOKE and DPEEK, and offsets that increase and decrease by 2 instead of 1. Quote Link to comment Share on other sites More sharing options...
stepho Posted May 11, 2022 Share Posted May 11, 2022 On 5/8/2022 at 9:03 AM, Rybags said: If you wanted a really long range then a LFSR simulation might be best. Beware that an LFSR sequence appears random but it repeats the same sequence over and over. so if one run gives 10-48-2-17-5-34 then the next run might give 3-19-10-48-2-17 Notice the run of 10-48-2-17 that is repeated. Quote Link to comment Share on other sites More sharing options...
Ricky Spanish Posted May 11, 2022 Author Share Posted May 11, 2022 Well in all fairness a sample size of 6 out of 10 would yield more repeatable numbers than say 6 out of 100000. Quote Link to comment Share on other sites More sharing options...
+MrFish Posted May 13, 2022 Share Posted May 13, 2022 On 5/8/2022 at 6:52 PM, MrFish said: I threw together a quick program to allow testing the method. The program lets you select a size, and choose whether you want to shuffle a freshly ordered array or reshuffle. Just for fun, I compiled it... runs at twice the speed, or faster. To run the compiled version, <BREAK> out of the uncompiled version once it's running. Then BRUN the "D:RUNTIME.EXE". Shuffle Deck.atr 1 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.