-6
import java.util.Scanner;
import java.util.Random;
public class blackJack {
public static void main(String[] args){
    Scanner input = new Scanner(System.in);
    int cards[] = {2,3,4,5,6,7,8,9,10};
    String card[] = {"H", "D", "C", "S"};
    String suit[] = {"A","K","Q","J"};
    //System.out.println(cards[2]);
    Random random = new Random();
    System.out.println(cards[random.nextInt(cards.length)] +card[random.nextInt(card.length)]);
}
}

I'm trying to create a random from cards and suit, because you can have either have a int of hearts, spades, clubs or diamonds or you can Ace of hearts, spades, clubs or diamonds.

Dmitry Avtonomov
  • 8,747
  • 4
  • 32
  • 45
user3421518
  • 3
  • 1
  • 4

4 Answers4

1

There's no reason to have separate arrays for face cards and lower-ranking cards. Make a single cards array hold all the card values and another to hold the suits:

public static void main(String[] args){
    Scanner input = new Scanner(System.in);
    String suits[] = {"H", "D", "C", "S"};
    String cards[] = {"2", "3", "4", "5", "6", "7", "8", "9", "10",
                      "J", "Q", "K", "A" };
    Random random = new Random();
    System.out.println(cards[random.nextInt(cards.length)]
        + suits[random.nextInt(suits.length)]);
}
Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
  • Oh thank you so much, I forgot that I don't need numbers as ints. – user3421518 Mar 19 '14 at 03:29
  • @user3421518 - If you do need card values (I suspect you eventually will, since your class is named "blackjack" :)) then you can keep a separate array of card values (e.g., `int[] values = {2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11}`). Of course, you'll need special handling for aces, since they can be 1 or 11. – Ted Hopp Mar 19 '14 at 03:31
0

There's zero reason why you need to have that integer/string dichotomy since you never need to treat them as integers - simply use strings for all values.

In addition, you may be better off (from a statistical distribution point of view) getting one random number and using that to select both values:

public static void main (String[] args) {
    Scanner input = new Scanner (System.in);
    String suits[] = {"H","D","C","S"};
    String cards[] = {"2","3","4","5","6","7","8","9","10","J","Q","K","A"};
    Random random = new Random ();
    int num = random.nextInt (cards.length * suits.length);
    System.out.println (cards[num % cards.length] + suits[num / suits.length]);
}

But keep in mind that this random card selection scheme only works if you have an infinite number of decks in your shoe. Casinos like to use multiple decks to reduce the impact of card counting but even they cannot do an infinite deck :-)

To properly emulate a casino, you would create a shoe structure containing x decks of cards which you would simply choose random ones from for dealing out (and removing from the shoe).

Then, when the shoe gets to a ore-determined size after a deal (eg, down to 20% of original size, another means by which casinos battle card counting), you reinvoke the "shuffle" - quoted since you needn't actually shuffle the cards when you can use Fisher Yates to shuffle on demand, as per another answer of mine here.

Community
  • 1
  • 1
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • How does using a single random number this way improve things from a statistical distribution point of view? It strikes me as exactly the same as picking a card value uniformly at random and then picking a suit uniformly at random. (Or are you assuming that sequential numbers produced by `Random` are somehow correlated?) – Ted Hopp Mar 19 '14 at 03:47
  • @Ted, statisticians will argue the case, but then they tend to be argumentative :-) I've had stats bods tell me that, because of the way many PRNGs work, it's better to hit the generator _once_ for a value rather than hitting it twice and combining them, partly because that matches the reality of choosing one card from 52 (if distribution is perfect this may not matter but what if every second value is even). That's why I said **may.** In any case, I'd prefer the better solution later in my answer of emulating a proper shoe rather than assuming you have an infinite supply of each card. – paxdiablo Mar 19 '14 at 03:51
  • Yeah, theoretically hitting a PRNG like `Random` once per result may be slightly better (but, as you say, that's easily debatable). However, if that's actually a concern, then one shouldn't be using `Random` at all and instead use a better RNG like `java.security.SecureRandom`. – Ted Hopp Mar 19 '14 at 04:12
0

I was trying to make this answer some sort of fun (in a positive way, not being a d***), but then I've noticed a terrible eclipse bug...

First, the answer (ok, not the answer, but my suggestion - just don't worry about the rand function, it's just fine)

public static void gen() {
        String card[] = {"A","2","3","4","5","6","7","8","9","10","J","Q","K"};
        String suit[] = {"\u2661", "\u2662", "\u2663", "\u2660"};
        System.out.println(
                card[(int)(Math.random()*card.length)] 
                        +suit[(int)(Math.random()*suit.length)]);

}

Now, the bug - ? while printing heart symbol

Eclipse simply does not accept the heart symbol! If I cut and paste from http://www.fileformat.info/info/unicode/char/2661/index.htm it simply works for all other suits, except hearts!

So I am sorry. This is the best I can do.

Community
  • 1
  • 1
Leo
  • 6,480
  • 4
  • 37
  • 52
-1

If you are concerned about the randomness of your card deck. Give a try at the fisher yates shuffling algorithm.

Java Fisher Yates

Davis Broda
  • 4,102
  • 5
  • 23
  • 37
MrMcKizzle
  • 150
  • 2
  • 10
  • Or just put the values into a `List` and use `Collections.shuffle()`. – Ted Hopp Mar 19 '14 at 03:44
  • True. Using your method guarantees that the items will shuffled correctly. Personally I would recommend implementing your own at first for fun. :) – MrMcKizzle Mar 19 '14 at 03:52