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
I used a 2-dimensional array to keep track of the suite at the same time.
two random statements, but then i didn't have to translate cards, ever!
wow they are really dumb over there. a mod had this to say about your code
Alkatran, this could take a while and thats the reason for shuffling rather then using your aproach.
what hell is he talking about?
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.
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 !
Last edited by dglienna; Dec 10th, 2004 at 10:39 PM.
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
I used a 2-dimensional array to keep track of the suite at the same time.
two random statements, but then i didn't have to translate cards, ever!
So much better written in java:
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
}
Looks like 9 lines... 4 if you only count the non-dimming lines.
Don't pay attention to this signature, it's contradictory.
Wait... how exactly is that stopping duplicate cards from being generated??
You forgot to dim ntry
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.
Sitting w/ Bob Status: -Next -To- Null- Friend: Philip
Posts
1,152
Re: 1000 lines
Why 8 lines?
dim blah, ha, gaga
1 line.
:::`DISCLAIMER`:::
Do NOT take anything i have posted to be truthful in any way, shape or form.
Thank You!
-------------------------------- "Never heard about "hiking" poles. I usualy just grab a stick from the nature, and use that as a pole." - NoteMe "Finaly I can look as gay as I want..." - NoteMe
Languages: VB6, BASIC, Java, C#. C++
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.
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.
Let's not forget that more lines pays more in some situations
Don't pay attention to this signature, it's contradictory.
Let's not forget that more lines pays more in some situations
I'd rather get paid for code quality and speed than the number of lines I write.
Anyway, here's a rough and ready .net project that has a nice PlayingCard class (what I wrote ). 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).