https://kotlinlang.org logo
Title
n

Nat Strangerweather

02/19/2022, 6:02 PM
Hi, I'm trying to get 49 tiles from the "letterBag" to put in a grid. I'm not sure what I'm doing wrong; there are two "y"s and I got three in my grid. Any ideas? Code in thread.
val letterBag =
        ("A".repeat(9) + "B".repeat(2) + "C".repeat(2) +
                "D".repeat(4) + "E".repeat(12) + "F".repeat(2) + "G".repeat(3) +
                "H".repeat(2) + "I".repeat(9) + "J".repeat(1) + "K".repeat(1) +
                "L".repeat(4) + "M".repeat(2) + "N".repeat(6) + "O".repeat(8) +
                "P".repeat(2) + "Q".repeat(1) + "R".repeat(6) + "S".repeat(4) +
                "T".repeat(6) + "U".repeat(4) + "V".repeat(2) + "W".repeat(2) +
                "X".repeat(1) + "Y".repeat(2) +
                "Z".repeat(1))
    val randomLetter = letterBag.random().toString()
LazyVerticalGrid(
            cells = GridCells.Fixed(7),
            verticalArrangement = <http://Arrangement.Top|Arrangement.Top>
        ) {
            items(49) { index ->
                Piece(index)
            }
        }
j

jbnizet

02/19/2022, 6:07 PM
random() picks letters... randomly. It won’t care if it has already picked two Ys before getting a third one. Just like when you roll a dice: you can have three 6 before getting your first 1.. If you want each element of your bag once an only once, in a random order, make it a list, shuffle the list, then iterate the list.
👍 1
n

Nat Strangerweather

02/19/2022, 6:09 PM
ok that's great, thanks for the explanation!