user12475206
user12475206

Reputation:

How to generate a random double value in a specific range using Kotlin?

How do I generate a random number in a specific float range (From 51.3257 to 52.4557 for example) using Kotlin?

var xCoord = randomValue()
var yCoord = randomValue()

Do I need to make a method or do I just import something?

Upvotes: 2

Views: 6517

Answers (4)

jenos kon
jenos kon

Reputation: 564

If you are using kotlin do this :

 var max=51.3257
 var min=52.4557
 var outpot= (min + Random.nextDouble() * (max - min))

Upvotes: 0

stegnerd
stegnerd

Reputation: 1189

Here is the documentation for that: https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.random/-random/index.html

I believe that it would be something like

import kotlin.random.Random

var xCoord = Random.nextDouble(51.3257, 52.4557)
var xCoord = Random.nextDouble(51.3257, 52.4557)

Upvotes: 13

KpStar
KpStar

Reputation: 132

Random r = new Random();
double randomValue = rangeMin + (rangeMax - rangeMin) * r.nextDouble();

Please try this. Thanks

Upvotes: -1

Wassim Ben Hssen
Wassim Ben Hssen

Reputation: 529

try this

double random = ThreadLocalRandom.current().nextDouble(51.3257, 52.4557);

Upvotes: 3

Related Questions