M
M
mk_qwerty2019-10-27 19:09:42
Java
mk_qwerty, 2019-10-27 19:09:42

How to generate a sequence of random non-repeating numbers?

Good day! There is such a task, to generate n hands of 5 cards, it is necessary that in each hand the cards are not repeated. How to implement non-repeating cards?
What I wrote, but with this decision, duplicate cards may appear in one hand.

public class deal{
  public static void main(String[] args){
    int n = Integer.parseInt(args[0]);

    String[] a  = {"Spades", "Clubs", "Hearts", "Diamonds"};
    String[] b = new String[14];

    b[1] = "Ace";
    b[2] = "King";
    b[3] = "Queen";
    b[4] = "Knave";
    b[5] = "Joker";

    for (int i = 6; i < b.length; i++){
      b[i] = "" + (i - 4);
    }

    for (int i = 0; i < n; i++){
      for (int j = 0; j < 5; j++){
        int suit = (int) (Math.random()*(a.length - 1.0) + 1.0);
        int value = (int) (Math.random()*((double) b.length - 1.0) + 1.0);

        System.out.println(b[value] + " of " + a[suit]);
      }
      System.out.println("\n");
    }
  }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
�
âš¡ Kotobotov âš¡, 2019-10-27
@mk_qwerty

you first need to generate a deck, and then take out (with removal) a random card from it,
until the deck is empty, take out a random card and add it to the hand.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question