My suggestion would be an extension function on IntRange to create randoms like this: (0..10).random()
Kotlin >= 1.3, multiplatform support for Random
As of 1.3, Kotlin comes with its own multi-platform Random generator. It is described in this KEEP. The extension described below is now part of the Kotlin standard library, simply use it like this:
val rnds = (0..10).random()
Kotlin < 1.3
Before 1.3, on the JVM we use Random or even ThreadLocalRandom if we're on JDK > 1.6.
fun IntRange.random() =
Random().nextInt((endInclusive + 1) - start) + start
Used like this:
// will return an `Int` between 0 and 10 (incl.)
(0..10).random()
If you wanted the function only to return 1, 2, ..., 9 (10 not included), use a range constructed with until:
Kotlin standard lib doesn't provide Random Number Generator API. If you aren't in a multiplatform project, it's better to use the platform api (all the others answers of the question talk about this solution).
But if you are in a multiplatform context, the best solution is to implement random by yourself in pure kotlin for share the same random number generator between platforms. It's more simple for dev and testing.
My implementation purpose nextInt(range: IntRange) for you ;).
Take care about my purpose, LCG is good for most of the use cases (simulation, games, etc...) but is not suitable for cryptographically usage because of the predictability of this method.
Possible Variation to my other answer for random chars
In order to get random Chars, you can define an extension function like this
fun ClosedRange<Char>.random(): Char =
(Random().nextInt(endInclusive.toInt() + 1 - start.toInt()) + start.toInt()).toChar()
// will return a `Char` between A and Z (incl.)
('A'..'Z').random()
For kotlinjs and other use cases which don't allow the usage of java.util.Random, this answer will help.
Kotlin >= 1.3 multiplatform support for Random
As of 1.3, Kotlin comes with its own multiplatform Random generator. It is described in this KEEP. You can now directly use the extension as part of the Kotlin standard library without defining it:
Another way of implementing s1m0nw1's answer would be to access it through a variable. Not that its any more efficient but it saves you from having to type ().
var ClosedRange<Int>.random: Int
get() { return Random().nextInt((endInclusive + 1) - start) + start }
private set(value) {}
My suggestion would be an extension function on IntRange to create randoms like this:
(0..10).random()
Kotlin >= 1.3, multiplatform support for Random
As of 1.3, Kotlin comes with its own multi-platform Random generator. It is described in this KEEP. The extension described below is now part of the Kotlin standard library, simply use it like this:
Kotlin < 1.3
Before 1.3, on the JVM we use
Random
or evenThreadLocalRandom
if we're on JDK > 1.6.Used like this:
If you wanted the function only to return
1, 2, ..., 9
(10
not included), use a range constructed withuntil
:If you're working with JDK > 1.6, use
ThreadLocalRandom.current()
instead ofRandom()
.KotlinJs and other variations
For kotlinjs and other use cases which don't allow the usage of
java.util.Random
, see this alternative.Also, see this answer for variations of my suggestion. It also includes an extension function for random
Char
s.Generate a random integer between
from
(inclusive) andto
(exclusive)You can create an extension function similar to
java.util.Random.nextInt(int)
but one that takes anIntRange
instead of anInt
for its bound:You can now use this with any
Random
instance:If you don't want to have to manage your own
Random
instance then you can define a convenience method using, for example,ThreadLocalRandom.current()
:Now you can get a random integer as you would in Ruby without having to first declare a
Random
instance yourself:Kotlin standard lib doesn't provide Random Number Generator API. If you aren't in a multiplatform project, it's better to use the platform api (all the others answers of the question talk about this solution).
But if you are in a multiplatform context, the best solution is to implement random by yourself in pure kotlin for share the same random number generator between platforms. It's more simple for dev and testing.
To answer to this problem in my personal project, i implement a pure Kotlin Linear Congruential Generator. LCG is the algorithm used by
java.util.Random
. Follow this link if you want to use it : https://gist.github.com/11e5ddb567786af0ed1ae4d7f57441d4My implementation purpose
nextInt(range: IntRange)
for you ;).Take care about my purpose, LCG is good for most of the use cases (simulation, games, etc...) but is not suitable for cryptographically usage because of the predictability of this method.
Possible Variation to my other answer for random chars
In order to get random
Char
s, you can define an extension function like thisIf you're working with JDK > 1.6, use
ThreadLocalRandom.current()
instead ofRandom()
.For kotlinjs and other use cases which don't allow the usage of
java.util.Random
, this answer will help.Kotlin >= 1.3 multiplatform support for Random
As of 1.3, Kotlin comes with its own multiplatform Random generator. It is described in this KEEP. You can now directly use the extension as part of the Kotlin standard library without defining it:
Another way of implementing s1m0nw1's answer would be to access it through a variable. Not that its any more efficient but it saves you from having to type ().
And now it can be accessed as such