All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.github.serpro69.kfaker.RandomService.kt Maven / Gradle / Ivy

Go to download

Generate realistically looking fake data such as names, addresses, banking details, and many more, that can be used for testing and data anonymization purposes.

There is a newer version: 2.0.0-rc.7
Show newest version
package io.github.serpro69.kfaker

import java.util.*

/**
 * Wrapper around [Random] that contains some extra helper functions.
 */
internal class RandomService(private val random: Random) {
    private val alphabeticSource = "abcdefghijklmnopqrstuvwxyz"

    fun nextInt() = random.nextInt()

    fun nextInt(bound: Int) = random.nextInt(bound)

    fun nextInt(intRange: IntRange): Int {
        val lowerBound = requireNotNull(intRange.minOrNull())
        val upperBound = requireNotNull(intRange.maxOrNull())

        return nextInt(lowerBound, upperBound)
    }

    fun nextInt(min: Int, max: Int) = random.nextInt(max - min + 1) + min

    fun  randomValue(list: List) = list[nextInt(list.size)]

    fun  randomValue(array: Array) = array[nextInt(array.size)]

    fun nextLetter(upper: Boolean): Char {
        val source = if (upper) alphabeticSource.toUpperCase() else alphabeticSource

        return source[nextInt(source.length)]
    }

    fun nextBoolean() = random.nextBoolean()

    fun nextLong() = random.nextLong()

    fun nextFloat() = random.nextFloat()

    fun nextDouble() = random.nextDouble()

    fun nextChar() = random.nextInt().toChar()

    fun nextString() = List(100) { nextInt().toChar().toString() }.joinToString("")
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy