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.

Code Snippets / A Quick Shuffle method..

Author
Message
David Gervais
Retired Moderator
19
Years of Service
User Offline
Joined: 28th Sep 2005
Location: Montreal, Canada
Posted: 1st Dec 2006 10:59 Edited at: 1st Dec 2006 11:05
Hi there,

I'm back to doing some coding and needed to remember how to shuffle a deck of cards. I finally remembered how from my old Atari Basic days. Here is a commented code snippet explaining the process. (Note this is not original code by me, but rather something from the good old days and I do not remember the original programmer.)



I hope this code snippet helps. Oh, and the 'size' of the deck can be mush larger if needed.

Cheers!

P.S. here is a small program with the routine as a function to test the shuffle code.




have a great day.
TEH_CODERER
21
Years of Service
User Offline
Joined: 12th Nov 2003
Location: Right behind you!
Posted: 4th Dec 2006 16:50
Simple when you know how but can be quite the puzzler when you try to get it for the first time. I'm sure this will help some people.

Mr Tank
21
Years of Service
User Offline
Joined: 25th Nov 2002
Location: United Kingdom
Posted: 5th Dec 2006 04:54
Nice example. Sorry to be a pedant, but shouldn't it be s=52 to 2, and rnd(s-1)+1?
At the moment the first random index you generate is between 1 and 53. I'm likely wrong and just need more sleep though.


You'll be able to click on this someday.
David Gervais
Retired Moderator
19
Years of Service
User Offline
Joined: 28th Sep 2005
Location: Montreal, Canada
Posted: 5th Dec 2006 12:25 Edited at: 5th Dec 2006 12:34
it took me awhile to grasp the quirks in most programming languages regardin '0' (zero) as the 'first number'. I make a habbit of always DIM'ing the number I want +1. this way I can avoid the use of '0'

ie:

Dim deck(53) this sets up a deck with yes 53 cards, but they are numbered from 0-52. programs like using '0' as the first number. I try to avoid it, because it confuses me. So, I then use 1-52 as my pointers and ignore the '0' slot.

and in the same way rnd(52) gives a number between 0 and 51. so by adding +1 it makes the number between 1 and 52.

anyways, I hope this helps clear up the code.

Cheers!

Edit, to clarify your question.. "but shouldn't it be s=52 to 2, and rnd(s-1)+1?"..

in your example rnd(s-1) would be rnd(51) which would be a number between 0 and 50 adding +1 gets it to 1-51.. you skip the 52nd slot. Do the math. and trust me. I've been all over this function. my first error was doing a DIM deck(52) it always gave me a 'out of range' error. it was trying to address deck(52) which did not exist due to the 'evil' Zero as the first digit in the array. LOL
Mr Tank
21
Years of Service
User Offline
Joined: 25th Nov 2002
Location: United Kingdom
Posted: 5th Dec 2006 17:58
I don't know about other languages, but i'm fairly sure that in DBP (at least version 1.06 that i'm using) you can use the nth slot- for example, you can

dim myarray(0)

and use it OK.

This code example should illustate my point. I declare an array of dimension 9, so it has 10 slots, running from 0 to 9. Note that in the second for/next loop, it definitely goes through all the numbers 0 to 9.
The program should hopefully show that the rnd(n) function generates a random integer from 0 to n.



You'll be able to click on this someday.
David Gervais
Retired Moderator
19
Years of Service
User Offline
Joined: 28th Sep 2005
Location: Montreal, Canada
Posted: 5th Dec 2006 21:54
Ouch, now my brain is imploding. I knew the first element in an array was '0' (zero) and had always taken a DIM xxx(10) to be 10 total elements numbered 0-9, now you say DIM xxx(10) is actually 11 elements?? that's just sick. it makes for very confusing array definitions.

I guess to preserve my sanity I now have to ignore both ends of an array lol.

Nuf said, Cheers!
Mr Tank
21
Years of Service
User Offline
Joined: 25th Nov 2002
Location: United Kingdom
Posted: 5th Dec 2006 22:00
If it makes your brain hurt, then you're in good company.
I only recently found out that if you create a bitmap measuring n by m, it has pixel coordinates ranging from (0,0) to (n-1,m-1), which is equivalent to what you reasonably assumed was going on with arrays.


You'll be able to click on this someday.
gearce
17
Years of Service
User Offline
Joined: 18th Dec 2006
Location: ex SCOTLAND, now MELBOURNE, Australia
Posted: 21st Dec 2006 08:14
Sorry David Gervais, but the programme as written causes Syntax Errors and will not run in DB Classic. Rem statements must be on a separate line. Whilst I acknowledge your comments "this is not original code by me, but rather something from the good old Atari Basic days", it is my understanding that anything appearing here should be written in DB language.

gearce
Robot
20
Years of Service
User Offline
Joined: 27th May 2004
Location:
Posted: 23rd Dec 2006 20:46
@Gearce It was put together for DBP but here's a what you would do for DBC. Corrected the arrays as well I think.



and

David Gervais
Retired Moderator
19
Years of Service
User Offline
Joined: 28th Sep 2005
Location: Montreal, Canada
Posted: 23rd Dec 2006 23:03
Sorry my bad, should have mentioned it was adapted to DBPro and not DBC. Oh and since I compiled and ran it no problem in DBPro I consider this code to be in the proper place. Also, I do not have DBC so I could not even try if it ran in that version of DB.

In My defense, this type of code is generic enough that any new programmer could easily adapt it to DBC, Playbasic or any other language.

Nuf said, Cheers!
gearce
17
Years of Service
User Offline
Joined: 18th Dec 2006
Location: ex SCOTLAND, now MELBOURNE, Australia
Posted: 25th Dec 2006 07:12
To David Gervais, I apologise. Like you don't have DB Classic and couldn't test the programme, I don't have DB Pro and couldn't test it. I moved the rem statements to individual lines and made a minor adjustment to the arrays and it works fine. Certainly a neater card shuffle than the one I had been using. Again, my apologies.

To Robot, thanks for your input. I didn't know rem statements could be written like this but I prefer to write them on individual lines. To each his own as the saying goes.

Let's leave it at that.

gearce

LANG MAY YER LUM REEK

Login to post a reply

Server time is: 2024-11-22 23:25:06
Your offset time is: 2024-11-22 23:25:06