Skip to content

Instantly share code, notes, and snippets.

@MehdiFal

MehdiFal/T.java Secret

Created March 20, 2019 13:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MehdiFal/03a2454c70548841538791a103a4df0b to your computer and use it in GitHub Desktop.
Save MehdiFal/03a2454c70548841538791a103a4df0b to your computer and use it in GitHub Desktop.
/*
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