mardi 11 février 2020

Kotlin: Returning random elements from a list

I'm having some trouble getting and returning random elements from a couple of lists (String and int). The problem is that every time I run the code, the values are indeed random. But for consecutive calls of the same method(s), the values are similar.

I've reduced my code down to the important parts, so there are no class definitions, imports etc. included in the sample here.

lonnsinntekter2 += TestsakDataBuilder.createInntekt(
    fnr = fnr, arbeid = TestsakDataBuilder.createArbeid()

fun createArbeid(arbeidsgiver: String = randomArbeidsgiver, stillingsprosent: Int = 
                               randomStillingsprosent,
                               erStillingFast: Boolean = HelperUtils.getRandomBoolean()): Arbeid {
    return Arbeid.builder()
        .arbeidsgiver(arbeidsgiver)
        .stillingsprosent(stillingsprosent)
        .erStillingFast(erStillingFast)
        .build()
    }   
}

The randomArbeidsgiver and randomStillingsprosent methods called from the above code:

val randomArbeidsgiver = listOf("Snusk & Fransen AS", "Humbug Elektro AS", "Hummer & Kanari ANS",
        "REMA1000", "Meny", "Gaukern på hjørnet", "Equinor", "Ad Libris").random()

val randomStillingsprosent = listOf(50, 60, 70, 80, 90).random()

What am I doing wrong here? Is it that the .random() function of the list selects the value at compile time, and not for each call?




Aucun commentaire:

Enregistrer un commentaire