JavaScript required
We’re sorry, but Coda doesn’t work properly without JavaScript enabled.
Skip to content
Gallery
Preparandkurs
Översikt
Schema
Behörighetstest
Lektionsmaterial
Övningsuppgifter
Logik
Verktyg / Program
Exempelkod
Versionshantering
Fullstack (Tech stack)
(Extra)
Kommentarer och Dokumentation
More
Share
Explore
Java
Random
Random random = new Random();
int number = random.nextInt(4);
System.out.println(number);
Denna kod ger ett värde ( som sätts i variabeln "number") som är mellan 0-4
This Random().nextInt(int bound) generates a random integer from 0 (inclusive) to bound (exclusive).
1.1 Code snippet.
For getRandomNumberInRange(5, 10), this will generates a random integer between 5 (inclusive) and 10 (inclusive).
private static int getRandomNumberInRange(int min, int max) {
if (min >= max) {
throw new IllegalArgumentException("max must be greater than min");
}
Random r = new Random();
return r.nextInt((max - min) + 1) + min;
}
Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
Ctrl
P
) instead.