Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Professional Discussion / How do I Compare Array Values?

Author
Message
Cruise McClarren
20
Years of Service
User Offline
Joined: 23rd Aug 2005
Location:
Posted: 2nd Aug 2010 14:24
More specifically, I want to have a deck of 52 cards (which I have stored numerically in an array). After randomly selecting 10 of those cards I want to make sure that no two numbers out of the 10 are the same as there are no duplicates in a deck of cards. But I can't figure out how to set up the array to check it for all values except the one I am comparing. FOr Example:

Now, I thought this would allow me to skip the number that I'm comparing and check it against the other ones that have not been checked yet. But it does not seem to work. Is there another way to do this without having to use all the code to separately check each of the values?

LIFE: "That thing that happens to us when we are too young to die"
~V.J.C. 2003
RedFlames
18
Years of Service
User Offline
Joined: 25th Aug 2007
Location: Germania
Posted: 2nd Aug 2010 15:28
How about doing it this way:

(Not tested or anything of course...)
Kevin Picone
23
Years of Service
User Offline
Joined: 27th Aug 2002
Location: Australia
Posted: 2nd Aug 2010 17:30


IanM
Retired Moderator
23
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 3rd Aug 2010 00:53
You can use a random shuffle:


Basically, it's pick a card from the 52, then swap it into position 1 in the array. Then pick a card from the remaining 51 and swap that into position 2. Then pick a card from the remaining 50 and swap that into position 3. Repeat until you have the number of cards you need in positions 1 to 10.

Just as you'd pick 10 random cards from the deck - you only pick from what is left unpicked which means that you don't need to check for duplicates at all.

BTW, the SWAP ARRAY ITEMS and SORT ARRAY RANGE are additions available via my plug-ins. If you don't want to use my plug-ins, then the swap is basically:


Login to post a reply

Server time is: 2026-07-25 08:29:25
Your offset time is: 2026-07-25 08:29:25