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

392
Visualizações
In kotlin, Is there a way to 'roll a die' each time an array is shuffled?

here is some sample code and what I'm trying to do.... I want to cycle through his list until each of the array items have been displayed, you display by clicking the issue is that the variables d6, d62 and d63 are only 'rolled' once when opening the program even if I put those variables into the shuffle function it still won't roll them again inside the list because the list is just copied into a new list at the program start... how can I roll the dice each time the list is shuffled?

    var countW = 0
    var d6 = Random().nextInt(6) + 1
    var d62 = Random().nextInt(6) + 1
    var d63 = Random().nextInt(6) + 1
    
    var testList = arrayListOf(
    "test $d6",
    "test $d62",
    "test $d63")
    
    var shuffledList = testList.shuffled()
    fun shuf() { shuffledList = testList.shuffled() }
    
        listButton.setOnClickListener {
        if (countW < 1) { shuf() }
        wResult.text = shuffledList.elementAt(countW)
        countW++
        if (countW >= shuffledList.count()) { countW = 0 }
    }
    
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I am quite uncertain of what you want exactly. But one thing is clear : the elements of your list are set one time at :

var testList = arrayListOf(
    "test $d6",
    "test $d62",
    "test $d63")

Changing d6, d62 and d63 doesn't update every string element of your ArrayList testList.

I don't know if knowing that resolves your problem, but here is a small (pretty simplistic) code snippet which can make you understand, would that be your problem.

/**
 * You can edit, run, and share this code.
 * play.kotlinlang.org
 */
import java.util.Random

fun main() {
    // var countW = 0
    var d6 = Random().nextInt(6) + 1
    var d62 = Random().nextInt(6) + 1
    var d63 = Random().nextInt(6) + 1


    var testList = arrayListOf(
        "test $d6",
        "test $d62",
        "test $d63");

    fun reRand(){
        d6 = Random().nextInt(6) + 1
        d62 = Random().nextInt(6) + 1
        d63 = Random().nextInt(6) + 1
        testList = arrayListOf(
        "test $d6",
        "test $d62",
        "test $d63");
    }


    var shuffledList = testList.shuffled();

    fun shuf() { shuffledList = testList.shuffled() }

    shuf()
    reRand()
    println(shuffledList)
    shuf()
    reRand()
    println(shuffledList)
    shuf()
    reRand()
    println(shuffledList)
    shuf()
    reRand()
    println(shuffledList)
}

You need to regenerate your list everytime, if that's how you want to do it. There are more efficient ways of doing it.

Edit : here is link to the Kotlin playground : https://pl.kotl.in/xRdGELkDb

about 4 years ago · Juan Pablo Isaza 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