To simulate shuffling a deck of cards.
http://www.visualbasicforum.com/show...678#post904678
I mean COME ON.
Printable View
To simulate shuffling a deck of cards.
http://www.visualbasicforum.com/show...678#post904678
I mean COME ON.
I used a 2-dimensional array to keep track of the suite at the same time.Code:Randomize
ReDim card(52)
ReDim picked(52)
x = 1
Do While x < 52
s = Int(Rnd * 52) + 1
If Not picked(s) Then
picked(s) = 1
card(x) = s
x = x + 1
End If
Loop
two random statements, but then i didn't have to translate cards, ever!
SEVEN!
Code:Dim Cards As New Collection
Randomize
On Error Resume Next
Do Until Cards.Count = 52
nTry = Int((52) * Rnd + 1)
Cards.Add nTry, CStr(nTry)
Loop
wow they are really dumb over there. a mod had this to say about your codewhat hell is he talking about?Quote:
Alkatran, this could take a while and thats the reason for shuffling rather then using your aproach.
that may take half a milisecound for a computer to do.
and whats does he mean thats the reason for shuffeling instead of useing your aproach you aporach does shuffel.
and hes even a mod thats just pathetic.
i could have eliminated two line for the picked() array, but MartinLiss has the most compact. I'm not that familar with Collections, but I have learned something. Thanks.
EDIT: I remember when it used to take about 5 seconds to shuffle a deck in quick basic. came down to a second or two under VB. now, it doesn't even register using the timer !
So much better written in java:Quote:
Originally Posted by dglienna
Looks like 9 lines... 4 if you only count the non-dimming lines. :afrog:Code://Head of program
import java.util.Random;
//Inside the class
Random rand = new Random();
//Inside your shuffle method
byte cards[] = new byte[52]
boolean picked[] = new boolean[52]
int n = 0;
for(int a = 1; a < 52; a++) { //52 cards
while (picked[n = rand.nextInt(52)]); //Get a new random card
picked[n] = true
cards[a] = n
}
Wait... how exactly is that stopping duplicate cards from being generated??Quote:
Originally Posted by MartinLiss
You forgot to dim ntry :p
Maybe we should also consider emulating real shuffling: Taken the existing pack and moving sections of cards... :rolleyes:
One of the features of a collection is that it throws an error if you try to add a duplicate key. That's why the On Error Resume Next statement is there. dglienna didn't Dim his variables so I did the same. If you want to say then that I have 8 lines that's OK with me.Quote:
Originally Posted by alkatran
Why 8 lines?
dim blah, ha, gaga
1 line. :ehh:
1000 lines is clearly excessive and written by a mentally damaged imbecile.
However, the number of lines cannot be a measure of efficiency. The bubblesort can be written in 10-15 lines but is probably the slowest sorting method around.
I have a VERY fast way of shuffling a deck of cards, but it takes roughly 20 or 30 lines to implement, with no messy collections or any overhead at all. If I posted it here it would no doubt be ignored in favour of Marty's 7 liner. :) someone can PM me if they want code.
Collections are fairly slow so there's no doubt that my code would be slower than most other ways, but as dglienna pointed out, for a small collection like this it hardly matters. BTW, you can use the Time processes using GetTickCount link in my signature to find out.
i think that i will start using that. the timer was too slow.
One line for the shuffling ;)Code:std::vector<card> deck;
// ...
std::random_shuffle(deck.begin(), deck.end());
Let's not forget that more lines pays more in some situations :rolleyes:Quote:
Originally Posted by wossname
I'd rather get paid for code quality and speed than the number of lines I write.Quote:
Originally Posted by alkatran
Anyway, here's a rough and ready .net project that has a nice PlayingCard class (what I wrote :D). Look at the Shuffle() sub in the form code for the shuffling alg. Feel free to keep the PlayingCard class and alter it as you wish.
The algorithm is adapted from a sample from "TAOCP" (Knuth 1997).