Results 1 to 16 of 16

Thread: 1000 lines

  1. #1

    Thread Starter
    Fanatic Member alkatran's Avatar
    Join Date
    Apr 2002
    Location
    Canada
    Posts
    860

    1000 lines

    To simulate shuffling a deck of cards.

    http://www.visualbasicforum.com/show...678#post904678

    I mean COME ON.
    Don't pay attention to this signature, it's contradictory.

  2. #2
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    12 lines!

    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
    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!

  3. #3

  4. #4
    Hyperactive Member dogfish227's Avatar
    Join Date
    Oct 2002
    Location
    GA
    Posts
    409

    Re: 1000 lines

    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.

    and hes even a mod thats just pathetic.

  5. #5
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: 1000 lines

    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.

  6. #6

    Thread Starter
    Fanatic Member alkatran's Avatar
    Join Date
    Apr 2002
    Location
    Canada
    Posts
    860

    Re: 12 lines!

    Quote Originally Posted by dglienna
    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
    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.

  7. #7

    Thread Starter
    Fanatic Member alkatran's Avatar
    Join Date
    Apr 2002
    Location
    Canada
    Posts
    860

    Re: 1000 lines

    Quote Originally Posted by MartinLiss
    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
    Wait... how exactly is that stopping duplicate cards from being generated??
    You forgot to dim ntry
    Last edited by alkatran; Dec 11th, 2004 at 09:35 AM.
    Don't pay attention to this signature, it's contradictory.

  8. #8

    Thread Starter
    Fanatic Member alkatran's Avatar
    Join Date
    Apr 2002
    Location
    Canada
    Posts
    860

    Re: 1000 lines

    Maybe we should also consider emulating real shuffling: Taken the existing pack and moving sections of cards...
    Don't pay attention to this signature, it's contradictory.

  9. #9
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: 1000 lines

    Quote Originally Posted by alkatran
    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.

  10. #10
    Frenzied Member ice_531's Avatar
    Join Date
    Aug 2002
    Location
    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++

  11. #11
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    Arrow Re: 1000 lines

    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.
    I don't live here any more.

  12. #12
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: 1000 lines

    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.

  13. #13
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: 1000 lines

    i think that i will start using that. the timer was too slow.

  14. #14
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: 1000 lines

    Code:
    std::vector<card> deck;
    // ...
    std::random_shuffle(deck.begin(), deck.end());
    One line for the shuffling
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  15. #15

    Thread Starter
    Fanatic Member alkatran's Avatar
    Join Date
    Apr 2002
    Location
    Canada
    Posts
    860

    Re: 1000 lines

    Quote Originally Posted by wossname
    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.

  16. #16
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    Re: 1000 lines

    Quote Originally Posted by alkatran
    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).
    Attached Files Attached Files
    I don't live here any more.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width