0

kotlin 获取随机字符串

ppp created at5 years ago view count: 3136
object Test1 {

    @JvmStatic fun main(args: Array<String>) {
        print(Test1.getRandomString(10))
    }

    private fun getRandom(count: Int): Int {
        return Math.round(Math.random() * (count)).toInt()
    }

    private val string = "1234567890abcdefghijklmnopqrstuvwxyz"

    private fun getRandomString(length: Int): String {
        val sb = StringBuffer()
        val len = string.length
        for (i in 0..length - 1) {
            sb.append(string[getRandom(len - 1)])
        }
        return sb.toString()
    }
}
report
回复

相关搜索关键词

Recent search keywords