Reputation:
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
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
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
Reputation: 132
Random r = new Random();
double randomValue = rangeMin + (rangeMax - rangeMin) * r.nextDouble();
Please try this. Thanks
Upvotes: -1
Reputation: 529
try this
double random = ThreadLocalRandom.current().nextDouble(51.3257, 52.4557);
Upvotes: 3