Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

219
Visualizações
How to get sufficient entropy for shuffling cards in Java?

I'm working on a statistics project involving cards and shuffling, and I've run across an issue with random number generation.

From a simple bit of math there are 52! possible deck permutations, which is approximately 2^226. I believe this means that I need a random number generator with a minimum of 226 bits of entropy, and possibly more (I'm not certain of this concept so any help would be great).

From a quick google search, the Math.random() generator in Java has a maximum of 48 bits of entropy, meaning that the vast majority of possible deck combinations would not be represented. So this does not seem to be the way to go in Java.

I was linked to this generator but it doesn't have a java implementation yet. Also for a bit of context here is one of my shuffling algorithms (it uses the Fisher-Yates method). If you have any suggestions for better code efficiency that would be fantastic as well.

public void shuffle(int type, int swaps){
    int[] newDeck = getNewDeck();

    if(type == 1){
        for(int i = 0; i < 52; i++){
            int nextCardIndex = (int)(Math.random()*newDeck.length);
            deck[i] = newDeck[nextCardIndex];
            newDeck = removeItem(nextCardIndex, newDeck);
        }
    }
}

public int[] getNewDeck(){
    int[] newDeck = new int[52];
    for(int i = 1; i <= 52; i++){
        newDeck[i-1] = i;
    }
    return newDeck;
}

public int[] removeItem(int index, int[] array){
    int[] newArray = new int[array.length-1];
    for(int i = 0; i < index; i++){
        newArray[i] = array[i];
    }
    for(int i = index; i < array.length-1; i++){
        newArray[i] = array[i+1];
    }

    array = newArray;
    return array;
}
over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Have you looked into the recent additions that are included in JDK 17?

https://docs.oracle.com/en/java/javase/17/core/pseudorandom-number-generators.html#GUID-08E418B9-036F-4D11-8E1C-5EB19B23D8A1

There are plenty of algorithms available:

https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/random/package-summary.html#algorithms

For shuffling cards you likely don't need something that is cryptographically secure.

Using Collections.shuffle should do the trick if you provide a decent RNG.

https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/Collections.html#shuffle(java.util.List,java.util.Random)

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda