Last active
July 26, 2019 07:11
-
-
Save bmaggi/0826256b494ccc346b22c196ec732a77 to your computer and use it in GitHub Desktop.
Java Random primitive generation (basic Api only)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.time.LocalDate; | |
import java.time.LocalDateTime; | |
import java.util.Random; | |
import java.util.UUID; | |
import java.util.concurrent.ThreadLocalRandom; | |
import java.time.Instant; | |
class JavaRandom { | |
public static void main(String[] args) { | |
// uuid | |
final UUID uuid = UUID.randomUUID(); | |
final long timestamp = UUID.randomUUID().timestamp(); | |
final String s = UUID.randomUUID().toString(); | |
// pseudo random using time | |
final Long datemilliseconds = System.currentTimeMillis(); | |
final long datenanoseconds = System.nanoTime(); | |
final LocalDateTime now = LocalDateTime.now(); | |
final LocalDate localDate = LocalDate.now(); | |
final Long instantMs = Instant.now().toEpochMilli(); | |
// Math | |
final double d = Math.random(); | |
// Random (many more options to get random values as stream random.doubles(). | |
final Random random = new Random(); | |
final int ri = random.nextInt(); | |
final double rd = random.nextDouble(); | |
final long r1 = random.nextLong(); | |
final boolean rb = random.nextBoolean(); | |
final float rf = random.nextFloat(); | |
// ThreadLocalRandom extends Random (same options) | |
final Long l = ThreadLocalRandom.current().nextLong(); | |
// random generation with simple tweak + API call | |
short rs = (short)random.nextInt(Short.MAX_VALUE + 1); | |
byte[] array = new byte[7]; random.nextBytes(array); | |
// using commons apache | |
//random String | |
int maxSize = new Random().nextInt(); | |
final String value = RandomStringUtils.random(maxSize); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment