Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,148,870 members, 7,802,802 topics. Date: Friday, 19 April 2024 at 09:59 PM

Method To Shuffle Cards In C# - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Method To Shuffle Cards In C# (3575 Views)

Creating UWP Application In C# Or C++ (xaml) / Tutorial: Building A Simple Fraction Arithmetic Program In C# Using TDD / [problem] Write A Program In C++ That Finds The Hcf Of 2 Numbers Without Using A Recursive Function (2) (3) (4)

(1) (Reply) (Go Down)

Method To Shuffle Cards In C# by mexzony: 5:53pm On Sep 23, 2011
@ all.
Am a beginning programmer.need ideas not code.
I have a deck contaning 5 card objects and i want to shuffle them in random order.heres what i came up with.
Pick a card and reference it then remove the card from the array.
Then generate a random position bearing in mind that the new position might contain a card so we reference that card too and remove it.
Then we set the first card to the new position.now i want the second card reference to be the basis for the next placement so as not to loose it by setting another refrence mistakenly using a for loop.u think a while loop will be good so that the second card will be placed until thsy are all shuffled b4 the loop terminates.pls be easy on me am still learning.thanks all of you.
Re: Method To Shuffle Cards In C# by whoelse(m): 7:12pm On Sep 23, 2011
mexzony:

@ all.
Am a beginning programmer.need ideas not code.
I have a deck contaning 5 card objects and i want to shuffle them in random order.heres what i came up with.
Pick a card and reference it then remove the card from the array.
Then generate a random position bearing in mind that the new position might contain a card so we reference that card too and remove it.
Then we set the first card to the new position.now i want the second card reference to be the basis for the next placement so as not to loose it by setting another refrence mistakenly using a for loop.u think a while loop will be good so that the second card will be placed until thsy are all shuffled b4 the loop terminates.pls be easy on me am still learning.thanks all of you.
You'll go far if u stick wit the bolded.

Back to your question, from what i can see, you've pretty much solved the problem. You pick card A and card B and switch positions and voila, ur done. Only thing then is, you can do this as many times as u like(this is where ur loop should come in. A for or while loop will suffice).
Re: Method To Shuffle Cards In C# by Fayimora(m): 10:24pm On Sep 23, 2011
The way i shuffle cards is kinda related to the selection sort algorithm. If you want to scatter, manipulate the way you arrange. . .  Anyways thats just my method.

Am SUPER happy that you actually don't want code! I love working with people like you,  keep it up!

Back on topic, am just going to explain my way. Firstly you have to understand the selection sort. When you do that, come back to read this algorithm.

The algorithm is very easy! Before i explain it in detail, am going to give you the very basic algorithm. Instead of moving the biggest item to the end of the list or beginning(depending on how you are to sort), you take a random element to the end!

Read the selection sort and understand how it works in DETAILS! Then tweak the algorithm to work the way i stated above.

Dont forget that you can tweak something else, doesn't have to be my way! If already know something like the bubble sort, you can also tweak that.

Goodluck!
Re: Method To Shuffle Cards In C# by ektbear: 4:29am On Sep 24, 2011
You could also generate a random number between [0,1] for each card.

Then sort the cards by the random # you generated.

This isn't as cheap as cycling through each entry and sending to a random location, though (which is what you seem to be suggesting in the original post.)
Re: Method To Shuffle Cards In C# by ektbear: 9:22am On Sep 24, 2011
The swapping approach you suggested is the cheapest, O(N). The one I suggested is NlogN, so isn't as cheap.

Basically what you can do is something like:

for element i in (1,2,3, . . ., N) of the list
    Generate a random integer j in (i, i+1, i+2, . . . , N)
    Take this random integer j and swap A_j with A_i

If you do this, then your new list is randomly permuted. A_1 is a random element from your original list, A_2 is a random element of the remaining elements of the original list, etc.
Re: Method To Shuffle Cards In C# by mexzony: 3:33pm On Sep 25, 2011
@ all
WOW thanks a whole lot guys.i finally got it working just now.it really drove me crazy but it was worth it.i spent two days on a small shufle() method,well at least i was able to come up with a solution.i simply created a new array took a random card from the old card array and placed it in the new card,did it for all cards and dispalyed the new card array.it may not be the best solution but when i tested it it really shuffled well to my satisfaction.am glad i did it on my own even if i was fraustrated at times.its worth it.thanks all.
Re: Method To Shuffle Cards In C# by kunlekunle: 5:07pm On Sep 25, 2011
are u programming black jack or cashie?
Re: Method To Shuffle Cards In C# by mexzony: 7:14pm On Sep 25, 2011
kunlekunle:

are u programming black jack or cashie?
not really i am just practising to improve my skills where i build a deck of cards and do what u will normaly do to a deck of cards like shuffle,move cards from one deck to another.its simply just playing around u know to test myself.
Re: Method To Shuffle Cards In C# by kunlekunle: 5:21am On Sep 26, 2011
ekt_bear

i beg, can you program black jack
i need someone who can do cashie - nigerian black jack
Re: Method To Shuffle Cards In C# by ektbear: 7:29am On Sep 26, 2011
kunlekunle:

ekt_bear

i beg, can you program black jack
i need someone who can do cashie - nigerian black jack

I don't know anything about black jack, bro. Sorry undecided

(1) (Reply)

How do I reverse a paragraph with Java? / Naija Coder: Test Your Coding Skills Now / Can Somebody Tell Me The Object-oriented Way Of Becoming Wealthy?

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 22
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.