Java Code 2


SUBMITTED BY: assassin413

DATE: Oct. 7, 2016, 2:05 p.m.

FORMAT: Text only

SIZE: 733 Bytes

HITS: 486

  1. public class RandomString {
  2. private static final char[] symbols;
  3. static {
  4. StringBuilder tmp = new StringBuilder();
  5. for (char ch = '0'; ch <= '9'; ++ch)
  6. tmp.append(ch);
  7. for (char ch = 'a'; ch <= 'z'; ++ch)
  8. tmp.append(ch);
  9. symbols = tmp.toString().toCharArray();
  10. }
  11. private final Random random = new Random();
  12. private final char[] buf;
  13. public RandomString(int length) {
  14. if (length < 1)
  15. throw new IllegalArgumentException("length < 1: " + length);
  16. buf = new char[length];
  17. }
  18. public String nextString() {
  19. for (int idx = 0; idx < buf.length; ++idx)
  20. buf[idx] = symbols[random.nextInt(symbols.length)];
  21. return new String(buf);
  22. }
  23. }

comments powered by Disqus