Solved

why doesnt my random number generator method work? just keeps putting out same #?

Posted on 2004-08-15
10
217 Views
Last Modified: 2010-03-31
     import java.io.*;
import java.util.*;

//***************************************************************
// Program that simulates the game of Black Jack between one player and
// a simulated dealer.  
//***************************************************************
class Blackjack

{
      public static void main(String args[])throws IOException
      {
      BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
      
      final int blackjack=21;
      int pcard1=0, pcard2=0, pcard3=0, pcard4=0, pcard5=0, dcard1=0, dcard2=0, dcard3=0, dcard4=0, dcard5=0;
      int dealer=0, player=0, count=0;
      char face, another;

//*************************************************************************
//21Dealing first hand
//*************************************************************************      
      
      System.out.println("Player      Dealer");
      for (count=0; count<2; ++count)
      {  pcard1=deal();
         System.out.print(" \t"+pcard1);
      }
      for (count=0; count<2; ++count)
      {  dcard1=deal();
         System.out.print("\t"+dcard1);
      }            

       static int deal()
      {
      Random generator = new Random();
      int card;

      card = generator.nextInt(13) + 1;

      return card;
      } // method deal
0
Comment
Question by:hedge243
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 11805974
Create your Random once in the constructor of Blackjack
0
 

Author Comment

by:hedge243
ID: 11806006
can you show me what you mean, I am very new at this.
0
 
LVL 92

Expert Comment

by:objects
ID: 11806008
static int deal()
{
     int card = (int) Math.random()*13 + 1;

     return card;
} // method deal
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 92

Expert Comment

by:objects
ID: 11806015
missed some brackets, that should be:

static int deal()
{
     int card = (int) (Math.random()*13) + 1;

     return card;
} // method deal

0
 
LVL 86

Accepted Solution

by:
CEHJ earned 100 total points
ID: 11806038
>>can you show me what you mean, I am very new at this.

Yes, something like this. Java is an object-oriented language and you should try to produce object-oriented code, i.e. non-procedural:


import java.io.*;
import java.util.*;

//***************************************************************
// Program that simulates the game of Black Jack between one player and
// a simulated dealer.
//***************************************************************
class Blackjack {
      private Random dealer;

      public Blackjack() {
            dealer = new Random();
      }

      public static void main(String args[]) throws IOException {
            BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

            final int blackjack = 21;
            int pcard1 = 0;
            int pcard2 = 0;
            int pcard3 = 0;
            int pcard4 = 0;
            int pcard5 = 0;
            int dcard1 = 0;
            int dcard2 = 0;
            int dcard3 = 0;
            int dcard4 = 0;
            int dcard5 = 0;
            int dealer = 0;
            int player = 0;
            int count = 0;
            char face;
            char another;

//*************************************************************************
//21Dealing first hand
//*************************************************************************
    Blackjack bj = new Blackjack();
            System.out.println("Player     Dealer");
            for (count = 0; count < 2; ++count) {
                  pcard1 = bj.deal();
                  System.out.print(" \t" + pcard1);
            }
            for (count = 0; count < 2; ++count) {
                  dcard1 = bj.deal();
                  System.out.print("\t" + dcard1);
            }
      }

      public int deal() {
            return dealer.nextInt(13) + 1;
      }
      // method deal
}
0
 
LVL 92

Expert Comment

by:objects
ID: 11806108
Simply replacing your deal method with the one i posted above will fix it.
Moving where u create Random will not make any difference, in fact you don't need to create one at all.

0
 

Author Comment

by:hedge243
ID: 11806768
so I need to make sure I know what Im doing, do my notes look right?

mport java.io.*;
import java.util.*;

//***************************************************************
// Program that simulates the game of Black Jack between one player and
// a simulated dealer.
//***************************************************************
class Blackjack
{
     private Random dealer;

     

     public static void main(String args[]) throws IOException {
          BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

          final int blackjack = 21;
          int pcard1 = 0;
          int pcard2 = 0;
          int pcard3 = 0;
          int pcard4 = 0;
          int pcard5 = 0;
          int dcard1 = 0;
          int dcard2 = 0;
          int dcard3 = 0;
          int dcard4 = 0;
          int dcard5 = 0;
          int dealer = 0;
          int player = 0;
          int count = 0;
          char face;
          char another;

//*************************************************************************
//21Dealing first hand
//*************************************************************************
    Blackjack bj = new Blackjack();
          System.out.println("Player     Dealer");
          for (count = 0; count < 2; ++count) {
               pcard1 = bj.deal();
               System.out.print(" \t" + pcard1);
          }
          for (count = 0; count < 2; ++count) {
               dcard1 = bj.deal();
               System.out.print("\t" + dcard1);
          }
     } // method main


//****************************************************************************
//method to choose random number or instantiate an object?
//****************************************************************************
      public Blackjack()
      {
          dealer = new Random();
      } // method Blackjack

//****************************************************************************
// method to return # from 1 to 13
//****************************************************************************
     public int deal()
     {
        return dealer.nextInt(13) + 1;
     }// method deal

     
} // class Blackjack
0
 
LVL 92

Expert Comment

by:objects
ID: 11806783
Simpler just to not worrying about Using Randon class at all, There is a static method Math.random() that does all you need. See the code I costed above for how to use it.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11808024
>>//method to choose random number or instantiate an object?

should probably read

// Instantiate a Blackjack instance and create a Random as 'dealer' to deal random cards

Incidentally, shouldn't deal be producing 2 to 14? (I don't know the rules of blackjack)
0
 
LVL 3

Expert Comment

by:RuadRauFlessa
ID: 11825995
CEHJ,
Looking at his deal function it looks correct since "A" = 1 or 11 and everything above "10" from there on out = 10

It all actually depends on how you want to represent the whole thing

if you say 1 = 2, 2 = 3 .... 12 = "Q", 13 = "K", 14 = "A"

Then you would be correct.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
sql import cannot be resolved jsp 3 76
Why doesn't this text field show up on my Applet frame? 2 28
Java Eclipse Loop 3 31
import as existing maven project 3 41
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question