Results 1 to 8 of 8

Thread: You horny little thing, you!

  1. #1
    wossname
    Guest

    You horny little thing, you!

    Fast, clean, non-duplicating array randomiser. This rules.


    Code:
    shuffle(int cards)
    {
    int x,tmp;
    x=cards;
    while(x--) CARDS[x]=x%52;
    while(cards)
    {
    x=random(cards--);
    tmp=CARDS[cards];
    CARDS[cards]=CARDS[x];
    CARDS[x]=tmp;
    }
    }
    Courtesy: J Stadolnik.

  2. #2
    jim mcnamara
    Guest
    It also ruled when published in Algol 45 years ago. See Knuth's 'The Art of Programming'

  3. #3
    Frenzied Member nishantp's Avatar
    Join Date
    Jan 2001
    Location
    Where you least expect me to be
    Posts
    1,375
    Do you have that code in VB by any chance?
    You just proved that sig advertisements work.

  4. #4
    wossname
    Guest

    Re: You horny little thing, you!

    I'll try and do a quick translation, I haven't tested it though...

    Code:
    public CARDS(0 to 51) as integer
    
    sub shuffle(byval cards as integer)
    
    dim x as integer,tmp as integer
    
    for x = 0 to cards - 1
    CARDS(x) = x mod 52
    next x
    
    while cards > -1
    'pick random card
    x = int(rnd*cards)
    cards = cards - 1
    'swap cards
    tmp = CARDS(cards)
    CARDS(cards) = CARDS(x)
    CARDS(x) = tmp
    
    loop
    
    End Sub
    After calling this, the CARDS() array should be re-arranged to a new sequence of numbers.

    As i said its not tested so it might be a bit buggy, just compare the code line for line with the C++ version to see the translation.

  5. #5
    Registered User Nucleus's Avatar
    Join Date
    Apr 2001
    Location
    So that's what you are up to ;)
    Posts
    2,530
    If you just want to shuffle a 1D string array maybe this will work?

    VB Code:
    1. Private Sub Command1_Click()
    2. Dim s(9) As String, i&, randomIndex&, tempVal$
    3.  
    4. For i = 0 To 9
    5.     s(i) = "Cogigate " & i & " times"
    6. Next
    7.  
    8. For i = 9 To 0 Step -1
    9.     Do
    10.         Randomize
    11.         randomIndex = Int(10 * Rnd)
    12.     Loop Until randomIndex <> i
    13.    
    14.     'Now we have the current index and a random index that are different
    15.     'Swap the values of the random index and the current index
    16.     tempVal = s(i)
    17.     s(i) = s(randomIndex)
    18.     s(randomIndex) = tempVal
    19. Next
    20.  
    21. For i = 0 To 9
    22.     Debug.Print s(i)
    23. Next
    24.  
    25. End Sub

  6. #6
    DerFarm
    Guest
    I did a bunch of work on this type of thing about 20 years ago. I don't have any code, BUT this advice:

    If you are really dealing cards (say for a bridge game) generally, the next thing you have to do is to sort them in each hand.

    Instead of dealing the cards, deal the SEATS. Then, when you construct the hands the cards are in order.

  7. #7
    wossname
    Guest
    Seats?

  8. #8
    DerFarm
    Guest
    If you start out with 1-52 cards and you have to deal
    without replacement you end up with a random placement of
    cards in a string/array/whatever. If you have dealt them to 4
    players in a card game and you are going to have human
    interaction on the screen, you then have to sort them by suit and
    denomination. Very time consuming and somewhat tricky.

    Assuming that you are dealing to 4 putative players they can be
    labelled N,E,W,S (north, east,west,south). In this case, you
    randomly determine which of the 4 (or however manyplayers) get
    the CARD.

    When you end up you have the original deck 1-52 in order, and a
    string or array of WHO GETS THE CARD. To build the presentation
    hand, you then only have to walk down the list assigning each.

    There are also some significant gains in the actual mechanics of
    the logic of computer play (ex: second hand low, cover an honor
    with an honor, .....)

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