Skip to content

Instantly share code, notes, and snippets.

@pablitar
Created April 12, 2017 20:54
Show Gist options
  • Save pablitar/309a55cfbbcba1052c44d290836d0264 to your computer and use it in GitHub Desktop.
Save pablitar/309a55cfbbcba1052c44d290836d0264 to your computer and use it in GitHub Desktop.
package ar.pablitar.random
import java.util.Random
import java.util.List
class RandomExamples {
static val random = new Random
def static randomBetween(Integer min, Integer max) {
val difference = max - min + 1
random.nextInt(difference) + min
}
def static randomBetween(Float min, Float max) {
val difference = max - min
random.nextFloat * difference + min
}
def static void main(String[] args) {
val palabras = #["Caballo", "Camello", "Cotorra", "Castor", "Carancho"]
(1..100).forEach[
println("Un número entre 0 y 9: " + random.nextInt(10))
println("Un número entre 3 y 15: " + randomBetween(3, 15))
println("Un flotante entre 0 y 25.5: " + random.nextFloat * 25.5)
println("Un flotante entre 5 y 8: " + randomBetween(5.0f, 8.0f))
println("Un random de entre una lista de elementos: " + randomIn(palabras))
]
}
def static <T>randomIn(List<T> lista) {
lista.get(random.nextInt(lista.size))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment