Re: cards out of wack

From: Lee Weiner (lee_at_nospam.org)
Date: 04/07/04


Date: Wed, 07 Apr 2004 04:59:56 GMT

It's because you're recreating the Random object every time you create one of
your cards. The Random object uses the computer's time as a seed and,
normally, you're twenty cards are being dealt with the same random seed, hence
the same values produced. Sometimes, the clock ticks in the middle of your
loop, and the values change. The solution is to instantiate the Random object
in main(), not in the card class, and pass a reference to it to the card class
in the constructor. Now in the card class, you'll get new numbers every time
you call nextInt().

Lee Weiner
lee AT leeweiner DOT org

In article <3340c7ee.0404061905.88dd7f1@posting.google.com>,
luke.bergen@verizon.net (Luke Bergen) wrote:
>hi, I'm trying to put together the constructs of a card game. this is
>what I have so far, given that System.out.print and System.out.println
>are functions that I've imported that output to screen:
>
>public class dealer
>{
> public static void main(String args[])
> {
> card myCard;
> for (int i = 0 ; i < 20 ; i++)
> {
> myCard = new card();
> myCard.printCard();
> }
> }
>}
>////////////////// EOF \\\\\\\\\\\\\\\\\\
>
>
>/////////// card.java \\\\\\\\\\\\\\\\\
>import java.util.Random;
>
>class card
>{
> final public int JACK = 11; // these next 8 variabls will
> final public int QUEEN = 12; // just make life simpler
> final public int KING = 13; // later on, rather than remembering
> final public int ACE = 1; // what number was what suit etc.
> final public int SPADE = 0; // we are making them public so
> final public int DIAMOND = 1; // that whatever program imports
> final public int CLUB = 2; // this class won't have to worry
> final public int HEART = 3; // about the numbers either.
>
> // face, ace = 1, 2 = 2, 3 = 3, etc...
> private int face;
> // we will make suit be a random number 0, 3
> // fourtunatly though, we don't really even need
> // to remember which is which suit though
> private int suit;
>
> Random gen;
>
> // I know it's not totally neccisary now, but just so it
> // will be easyer to expand on, I'm keeping all the initializing
> // in the home-made constructer
> public card ()
> {
> gen = new Random();
> face = (gen.nextInt(13) + 1); // 1 = ace, and so on
> suit = gen.nextInt(3); // set the suit
> }
>
> public void printCard()
> {
> String sFace; // must be string in case of jack, ace, queen, or
>king
> switch (face)
> {
> case JACK:
> sFace = "Jack";
> break;
> case QUEEN:
> sFace = "Queen";
> break;
> case KING:
> sFace = "King";
> break;
> case ACE:
> sFace = "Ace";
> break;
> default: // it's none of the weird cards so
> sFace = "N"; // we will test for "N" in output
> break; // and output acordingly if need be
> }
>
> // now for the suit
> String outSuit = "";
> switch (suit)
> {
> case SPADE:
> outSuit = "Spades";
> break;
> case DIAMOND:
> outSuit = "Diamonds";
> break;
> case CLUB:
> outSuit = "Clubs";
> break;
> case HEART:
> outSuit = "Hearts";
> break;
> default:
> outSuit = "oops, I'm sorry, an error occured somewhere";
> break;
> }
> if (sFace == "N")
> System.out.print(face + " of ");
> else
> System.out.print(sFace + " of ");
> System.out.println(outSuit);
> }
>}
>///////////////////////////// EOF \\\\\\\\\\\\\\\\\\\\\\\\\\\\\
>
>
>
>but for some reason my output has a random card, but it will be that
>same random card for every iteration of the loop, sometimes it will
>change to a different card midway through the loop but usially it's
>the same the whole way. now whats weird is if I run this program
>twice, really fast it will sometimes have the same random card between
>runs, in other words
>run1:
>ace of spades
>ace of spades
>ace of spades
>ace of spades
>ace of spades
>ace of spades
>ace of spades
>ace of spades
>ace of spades
>ace of spades
>ace of spades
>ace of spades
>ace of spades
>ace of spades
>ace of spades
>ace of spades
>ace of spades
>ace of spades
>ace of spades
>ace of spades
>run2: run just after run1 has finished
>ace of spades
>ace of spades
>ace of spades
>ace of spades
>ace of spades
>ace of spades
>ace of spades
>ace of spades
>ace of spades
>ace of spades
>some other card
>some other card
>etc.
>
>it's weird, I don't understand why it's not working the way I want it
>to.
>any thoughts on this would be greatly appreciated, thanks in advance.
>- Traddles



Relevant Pages

  • Re: Easy probability question (Bayes theorem)
    ... an ace of diamonds out of a deck of cards ... is it asking for the aces of spades and diamonds to be ... first card is one of those, that the second is the other one is 1/51. ...
    (sci.math)
  • Re: prob. problem
    ... the first card will be the two of clubs. ... Running total: 5% ace of spades. ...
    (rec.gambling.poker)
  • Re: prob. problem
    ... the first card will be the two of clubs. ... Running total: 5% ace of spades. ...
    (rec.gambling.poker)
  • Re: what does 4D mean on this sequence?
    ... You have an ace and a diamond more than a 2D bid. ... and then you could jump to 6 clubs indicating grand slam hopes. ... So at slam you lose the Ace of spades and have to finesse the ...
    (rec.games.bridge)
  • Re: Another trial
    ... >> Diamond lead to the ace. ... I must confess I'd bang down the AK of trump and start the spades. ... RHO has the stiff ace of spades. ...
    (rec.games.bridge)

Loading