-
-
Save MehdiFal/03a2454c70548841538791a103a4df0b to your computer and use it in GitHub Desktop.
This file contains 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
/* | |
what you need is something that randomly picks one of these: 0-9, 65-90, 95 | |
if number is < 10 => digit, if above it's letter which means you'll cast it to char | |
means we have 38 possibilities, or 38 + 26 if we wanna include lower case letters | |
*/ | |
List<Integer> lst = new ArrayList<>(38); | |
lst.addAll(0, IntStream.rangeClosed(0, 9).boxed().collect(Collectors.toList())); | |
lst.addAll(10, IntStream.rangeClosed(65, 90).boxed().collect(Collectors.toList())); | |
lst.add(95); | |
int random = ThreadLocalRandom.current().nextInt(0, 38); | |
char result; | |
if (random < 10) { | |
result = Character.forDigit(random, 10); | |
} else { | |
result = (char)((int)lst.get(random)); | |
} | |
System.out.println(result); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment