hedge243
asked on
why doesnt my random number generator method work? just keeps putting out same #?
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.i n));
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"+dcar d1);
}
static int deal()
{
Random generator = new Random();
int card;
card = generator.nextInt(13) + 1;
return card;
} // method deal
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.i
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
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"+dcar
}
static int deal()
{
Random generator = new Random();
int card;
card = generator.nextInt(13) + 1;
return card;
} // method deal
Create your Random once in the constructor of Blackjack
ASKER
can you show me what you mean, I am very new at this.
static int deal()
{
int card = (int) Math.random()*13 + 1;
return card;
} // method deal
{
int card = (int) Math.random()*13 + 1;
return card;
} // method deal
missed some brackets, that should be:
static int deal()
{
int card = (int) (Math.random()*13) + 1;
return card;
} // method deal
static int deal()
{
int card = (int) (Math.random()*13) + 1;
return card;
} // method deal
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
Moving where u create Random will not make any difference, in fact you don't need to create one at all.
ASKER
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.i n));
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
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.i
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
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
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.
>>//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)
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)
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.
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.