Board Logo
« SHUFFLING (Cards, Questions, Images) »

Welcome Guest. Please Login or Register.
Jan 18th, 2018, 3:54pm


Conforums Terms of Service | Membership Rules | Home | Search | Recent Posts | Notification | Format Your Message | Installation FAQ

This board is not meant for general discussion, it is meant for posting articles to help others.
For general discussions use the appropriate board, which best describes your problem area.

« Previous Topic | Next Topic »
Pages: 1 2 3  Notify Send Topic Print
 veryhotthread  Author  Topic: SHUFFLING (Cards, Questions, Images)  (Read 1869 times)
bplus
Senior Member
ImageImageImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 1255
xx Re: SHUFFLING (Cards, Questions, Images)
« Reply #30 on: Sep 14th, 2015, 08:12am »

on Sep 14th, 2015, 01:11am, tsh73 wrote:
Hello bplus,

Actually, that's leftover from debugging.
But it has it's purpose - if you run program as is, you'll get exactly same results as mine.

Sure I did.
There 8*10^67 shuffles of 52 cards, and only 24 for 4 cards.
So I changed it to work for 4 cards.
52 to 4, 53 to 5.
Dit I broke it?
If not - is there reason it works bad for 4 cards but will work good for 52 cards?


Are my eyes deceiving me or was there an edit made? And what happened to my post about Fisher Yates and code?
That was right where tsh73 reply is now? Wayne appears to be responding exactly to what I had posted. Was it rude of me to post right after I had posted a reply to ez? or just an error in editing on forum?

Wayne Quote:
Quote:
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)


I'll admit that going from indices 0 to n-1 and then counting down from n-1 to 1 doesn't make sense to me. Seems like it could possibly leave the lowest card unmoved.
Also, the whole ≤ thing doesn't make much sense, either. I realize that it is there to force a card to be moved; it can't remain in its original position. But that defeats true randomness. To be truly random, the possibility has to exist that the deck could end up in its original configuration. The odds against that would be astronomically high, but it needs to be possible.
(Even with the ≤ bit, it is still possible for a later operation to put the card back to its original position - but only if it was placed lower in the deck.)

Mine goes through all 52 indices (well, it excludes the 53rd index, which is 0) and swaps them with randomly selected cards.
This means that the further you progress through the deck, the greater probability that you are swapping a card that has already been moved. As you pass the halfway-point, you are more likely to swap the card that has already been moved with another card that has already been moved. Sort of like a triple-shuffle.
Up to three shuffles in one pass! There's efficiency for you!


Anyway, I was going to suggest tsh73 modify the rest of Dealcard function just as it now shows on forum. I would then agree if the test fails, the function is incinerator material. Apparently it still does, OK then. Fair test, I think.
BTW I do understand now why tsh73 used RANDOMIZE .5, so that I may test code under exact same conditions as tsh73 and I appreciate that!

I did check out Fisher Yates and from that code concluded it far superior to my Dealcard function because it does 0 hunting for cards. What a clever idea!

I did have code up to show that all the cards do get shuffled, a demo like I had for Dealcard test but it got disappeared huh It was very late into morning, I may have missed hitting post button but Wayne seems to responding to what I did have posted except he starts with quote from Fisher Yates link ez left.

To Wayne, assigning a card a random position and then doing it again is redundant, it effectively gets job done but not efficiently.

EDIT: Ah, I see, Wayne thinks counting down from n-1 to 1 is missing a card because usually "i" does index an array element. Actually here, "i" is indicating how many cards are left to assign a position. Notice that in every random assignment the lowest card is available to swap (if it hasn't been already).

MORE EDIT: BTW it is possible with this code to "swap" a card with itself, in other words leave card 52 right where it was before the "shuffle" that is why the ≤'s are needed in the algorithm description.
« Last Edit: Sep 14th, 2015, 10:26am by bplus » User IP Logged

B+
Rod
Administrator
ImageImageImageImageImage


member is offline

Avatar

Graphics = Goosebumps!


PM

Gender: Male
Posts: 3151
xx Re: SHUFFLING (Cards, Questions, Images)
« Reply #31 on: Sep 14th, 2015, 1:25pm »

Ha, who said shuffling was easy? Edits or not the concept will tease coders for years to come.

As a community we like debate and discussion and ideas and challenges. Some folks might post and regret and chop and change, but that's why we are here.

Chopping and changing posts is understandable but try and minimise it. Correct errors quickly but leave misunderstandings for folks to comment on and improve. That's the enjoyment of this forum. I have often been improved upon!



User IP Logged

bplus
Senior Member
ImageImageImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 1255
xx Re: SHUFFLING (Cards, Questions, Images)
« Reply #32 on: Sep 14th, 2015, 10:57pm »

A simple demo:

Code:
'shuffle test.txt for JB 2015-09-14 MGA/B+  Fisher-Yates or Knuth Method
dim deck(51)
for i=0 to 51
    deck(i)=i
next
while quit<>1
    call shuffle
    for i=0 to 51
        print right$("   "+str$(deck(i)),4);
        if i mod 13 =12 then print
    next
    print:input "To see another enter spacebar, to quit just enter.";again$
    if again$="" then quit=1
    print
wend
print"Goodbye"

sub shuffle
    for i=51 to 1 step -1
        r=int(rnd(0)*(i+1))
        t=deck(i)
        deck(i)=deck(r)
        deck(r)=t
    next
end sub
 

User IP Logged

B+
tsh73
JB-Supporter


member is offline

Avatar




PM

Gender: Male
Posts: 3636
xx Re: SHUFFLING (Cards, Questions, Images)
« Reply #33 on: Sep 15th, 2015, 03:01am »

There is a shorter way then
Code:
print right$("   "+str$(deck(i)),4); 

it's USING:
Code:
print using("####",deck(i)); 
User IP Logged

Q: "And if I took your codes and compile them, and sell them for a profit"?
A: Go ahead. I had my share of good then I coded it for fun, if you can make better use of it - please do.
(enjoying JB 1.01 on WinXP, netbook and desktop)
bplus
Senior Member
ImageImageImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 1255
xx Re: SHUFFLING (Cards, Questions, Images)
« Reply #34 on: Sep 15th, 2015, 10:34am »

on Sep 15th, 2015, 03:01am, tsh73 wrote:
There is a shorter way then
Code:
print right$("   "+str$(deck(i)),4); 

it's USING:
Code:
print using("####",deck(i)); 


YES, PRINT USING is shorter to type AND clearer to read and understand. (For cards, I think we would both use something else.)

BUT, (technically) is it faster?

Here is a test I just ran:
Code:
'f-y or knuth shuffle v2.txt for JB 2015-09-15 MGA/B+

' Fisher-Yates or Knuth Method but this is also what tsh73 is calling Modern Wiki Method! (I think)
' Well we are comparing apples to apples but calling one Golden and the other Delicious, ha!

'Rats, I should have included the deck setup in the loop in the simple demo to show the shuffle from ordered deck.
'PLUS, here I will generalize the shuffle to any size.

'PLUS, here I will time the PRINT USING method against my primitive " spacey " method
' copy paste code line: print right$("     "+str$(deck(i)),6);   'handle up to 5 digit
' copy paste code line: print using("######",deck(i));

' Results of timed tests a couple secs to calc shuffle, 3-4 minutes to printout on Windows Laptop 7-64 system w/AMD
'============================================ Touchpad active:   Touchpad deactivated:
'result with         using("######",deck(i));       247,230 ms             232,160 ms and 196,763
'result with right$("     "+str$(deck(i)),6);       230,179 ms             192,582 ms and 193,909

'n = number of items to shuffle the only number you need to adjust to try different size "decks"
n=99999
global nm1  'the only number needed for shuffle
'nm1 = n minus 1 for 0 based arrays
nm1=n-1
dim deck(nm1)
while quit<>1
    for i=0 to nm1
        deck(i)=i
    next
    call shuffle
    t=time$("ms")
    for i=0 to nm1
        print right$("     "+str$(deck(i)),6);
        if i mod 13 =12 then print               '6 x 13 =78 characters just fits across screen
    next
    t=time$("ms")-t
    print:print "The printout, in ms, took ";t
    print:input "To see another enter spacebar, to quit just enter.";again$  'really any key+enter
    if again$="" then quit=1
    print
wend
print"Goodbye"

sub shuffle
    for i=nm1 to 1 step -1
        r=int(rnd(0)*(i+1))
        t=deck(i)
        deck(i)=deck(r)
        deck(r)=t
    next
end sub
 


Disclaimer: Times will vary significantly between systems so it is important to compare times between different methods on one's own system but even then there are differences between runs. Therefore 3 tests for each method may be quite inadequate and only an indication of differences in method.
User IP Logged

B+
tsh73
JB-Supporter


member is offline

Avatar




PM

Gender: Male
Posts: 3636
xx Re: SHUFFLING (Cards, Questions, Images)
« Reply #35 on: Sep 15th, 2015, 5:43pm »

Quote:
BUT, (technically) is it faster?

LOL

If you have so much PRINT what you have to bother which is faster - you really just have too much PRINT! ;)

BTW it looks like
Code:
right$("     ";deck(i),6) 

is yet faster.
User IP Logged

Q: "And if I took your codes and compile them, and sell them for a profit"?
A: Go ahead. I had my share of good then I coded it for fun, if you can make better use of it - please do.
(enjoying JB 1.01 on WinXP, netbook and desktop)
bplus
Senior Member
ImageImageImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 1255
xx Re: SHUFFLING (Cards, Questions, Images)
« Reply #36 on: Sep 15th, 2015, 10:49pm »

on Sep 15th, 2015, 5:43pm, tsh73 wrote:
LOL

If you have so much PRINT what you have to bother which is faster - you really just have too much PRINT! ;)

BTW it looks like
Code:
right$("     ";deck(i),6) 

is yet faster.


YES "too much PRINT!"
YES "is yet faster."

Well I'll B;
User IP Logged

B+
Pages: 1 2 3  Notify Send Topic Print
« Previous Topic | Next Topic »

Conforums Terms of Service | Membership Rules | Home | Search | Recent Posts | Notification | Format Your Message | Installation FAQ

Donate $6.99 for 50,000 Ad-Free Pageviews!

| |

This forum powered for FREE by Conforums ©
Sign up for your own Free Message Board today!
Terms of Service | Privacy Policy | Conforums Support | Parental Controls