fork(7) download
  1. import java.security.SecureRandom;
  2. import java.util.Random;
  3.  
  4. public class Main {
  5.  
  6. private static final char[] CHARSET_aZ = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();
  7. private static final char[] CHARSET_AZ_09 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".toCharArray();
  8. private static final char[] CHARSET_HEX = "0123456789ABCDEF".toCharArray();
  9. private static final char[] CHARSET_SPECIAL = { '!', 'A', 'B' };
  10.  
  11. public static String randomString(char[] characterSet, int length) {
  12. Random random = new SecureRandom();
  13. char[] result = new char[length];
  14. for (int i = 0; i < result.length; i++) {
  15. // picks a random index out of character set > random character
  16. int randomCharIndex = random.nextInt(characterSet.length);
  17. result[i] = characterSet[randomCharIndex];
  18. }
  19. return new String(result);
  20. }
  21.  
  22. public static void main(String[] args) {
  23. System.out.println("using a-z A-Z");
  24. for (int i = 0; i < 5; i++)
  25. System.out.println(randomString(CHARSET_aZ, 30));
  26.  
  27. System.out.println("\nusing A-Z 0-9");
  28. for (int i = 0; i < 5; i++)
  29. System.out.println(randomString(CHARSET_AZ_09, 30));
  30.  
  31. System.out.println("\nusing HEX");
  32. for (int i = 0; i < 5; i++)
  33. System.out.println(randomString(CHARSET_HEX, 30));
  34.  
  35. System.out.println("\nusing SPECIAL");
  36. for (int i = 0; i < 5; i++)
  37. System.out.println(randomString(CHARSET_SPECIAL, 30));
  38.  
  39. System.out.println("\nincreasing length");
  40. for (int i = 0; i < 10; i++)
  41. System.out.println("len " + i + " => " + randomString(CHARSET_AZ_09, i));
  42. }
  43. }
  44.  
Success #stdin #stdout 0.11s 380352KB
stdin
Standard input is empty
stdout
using a-z A-Z
MtrWGSMAKufTmGBNWVStcBKIBViBDX
fdBffYYyQnhWKGRkktLFoWCViyWngL
mbWLXFKMpWTdWiQvngbdGxlMLscRvL
WlwPaRJEDPGmJgNjCZiTbmxdsTjObV
xLnQJIIlCKAyPzljPxtWDuKqgozSuZ

using A-Z 0-9
3MA5GS67710GCAI0PG8V8W9OKBHDPQ
SC4GOVNCTHHW9VS951HYKUEQ0I8V45
8J83G5W76UF2V74RNIBZRVE6YD6KV3
D4W15GSFPE95RIRSD26WJKVWZ8FEHE
GH6DAK1PS5V1JQEZXLQ16P2ZYEENLP

using HEX
BC9AA2716DB4446FDDFE6C8F6277D7
49E8B073E7FF0D1358A7B338F365DD
5D7DFC3AB4307017713AED1B3A8EFD
68E3FA49BDB4E537323F14360D34B2
C80DC049EE7A8905E367284EFE163D

using SPECIAL
A!B!!B!!!AABBAA!BBBA!!!!ABB!!!
!!A!!BAAA!!BABBABABAB!AB!BBABA
ABBBAAAB!!!AAABBBBA!BBABBA!BBB
!!AB!!!BABBBAA!AA!!!!!BB!B!ABB
A!B!BA!B!BBBBB!!BBAA!BAAAA!!AB

increasing length
len 0 => 
len 1 => H
len 2 => GX
len 3 => 53J
len 4 => SLKH
len 5 => UXNYR
len 6 => N48IGT
len 7 => 90OEO8F
len 8 => A1UZCJ8S
len 9 => GO9EJO1OZ