Thread: random number

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    45

    random number

    I am working on my own project that is eventually going to simulate a poker game, and give each player their % of winning.

    But I am a rather new programmer, so I am going to start out small. I am only going to have cards 1 through 4 and only two suites.

    What I need to do is randomly pick one of the 8 cards, then randomly pick another card. But every time a card is picked, eliminate it from the options.

    In other words, pick a card from one of the 8 possible cards, but dont pick the same card twice.

    thank you in advance.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Create a deck, which knows which cards have been dealt, and which cards remain.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    45
    and I would make a deck... how?

  4. #4
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    With structures.
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  5. #5
    Registered User
    Join Date
    Apr 2007
    Posts
    45
    thanks.. big help

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    card_t deck[52];
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Math wizard
    Join Date
    Dec 2006
    Location
    USA
    Posts
    582
    A bunch of flags. Ace through King take 0 through 12 in the array for one suit, then 13 through 25 for another suit, and so on. If the flag is 1, meaning the card is available, then it can be chosen. Once chosen, the flag is set to 0 to indicate it cannot be used again. If the random draw returns a card with the flag at 0, then the draw must be repeated until it gets something with a 1. When the card is put back, the flag is set back to 1 so it can be redrawn. Player hands could be done in a very similar way, except, perhaps, using a card ID instead to save on memory (as long as fewer than 6 cards are needed anyway).
    High elevation is the best elevation. The higher, the better the view!
    My computer: XP Pro SP3, 3.4 GHz i7-2600K CPU (OC'd to 4 GHz), 4 GB DDR3 RAM, X-Fi Platinum sound, GeForce 460, 1920x1440 resolution, 1250 GB HDD space, Visual C++ 2008 Express

  8. #8
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    Quote Originally Posted by s_jsstevens View Post
    thanks.. big help
    Well what did you expect, we're not going to give you flat out code. You asked how to make a deck, I said with structures and that was the right answer. Now, since what now I believe you meant to say is : "How do I represent a deck with structures ?" here's how you'd do that :

    All you do is have a card structure which is bascially 2 intergers, one interger represents the card's value, the other represent's the card's suit. Then, your deck would just be an array of 52 of these structures, as Salem has posted above.
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  9. #9
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    If I was to store flags, I wouldn't use an array of bytes, rather a bitset. Your then only using 7 bytes to store 52 "bytes" of flags.

    But I suppose this is over-complex. Stick with an array of structures as Salem said.

  10. #10
    Math wizard
    Join Date
    Dec 2006
    Location
    USA
    Posts
    582
    Yeah, using bitwise operators are a bit complex. Merely using an array with 52 items is the easiest way.
    High elevation is the best elevation. The higher, the better the view!
    My computer: XP Pro SP3, 3.4 GHz i7-2600K CPU (OC'd to 4 GHz), 4 GB DDR3 RAM, X-Fi Platinum sound, GeForce 460, 1920x1440 resolution, 1250 GB HDD space, Visual C++ 2008 Express

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. rapid random number generation problem
    By Newton in forum C Programming
    Replies: 17
    Last Post: 09-19-2008, 02:08 PM
  2. Random number in range generation.
    By hebali in forum C Programming
    Replies: 19
    Last Post: 03-04-2008, 10:46 AM
  3. adding a number to a number
    By bigmac(rexdale) in forum C Programming
    Replies: 11
    Last Post: 10-24-2007, 12:56 PM
  4. random number tutorial
    By 7stud in forum C++ Programming
    Replies: 3
    Last Post: 07-26-2005, 02:41 PM
  5. Random Number Generator
    By Ikurik in forum C++ Programming
    Replies: 16
    Last Post: 08-17-2003, 07:34 PM