Random Letter Class


/ Published in: Java
Save to your folder(s)

I did not code this, but it is a very handy, and easy-to-understand class which uses the random number generator to generate a random letter.


Copy this code and paste it in your HTML
  1. import java.util.Random;
  2.  
  3. public class ExtRandom extends Random {
  4.  
  5.  
  6. private String output = "";
  7.  
  8. public String nextLetter() {
  9.  
  10. switch (nextInt(26)) {
  11. case 0: output = "a"; break;
  12. case 1: output = "b"; break;
  13. case 2: output = "c"; break;
  14. case 3: output = "d"; break;
  15. case 4: output = "e"; break;
  16. case 5: output = "f"; break;
  17. case 6: output = "g"; break;
  18. case 7: output = "h"; break;
  19. case 8: output = "i"; break;
  20. case 9: output = "j"; break;
  21. case 10: output = "k"; break;
  22. case 11: output = "l"; break;
  23. case 12: output = "m"; break;
  24. case 13: output = "n"; break;
  25. case 14: output = "o"; break;
  26. case 15: output = "p"; break;
  27. case 16: output = "q"; break;
  28. case 17: output = "r"; break;
  29. case 18: output = "s"; break;
  30. case 19: output = "t"; break;
  31. case 20: output = "u"; break;
  32. case 21: output = "v"; break;
  33. case 22: output = "w"; break;
  34. case 23: output = "x"; break;
  35. case 24: output = "y"; break;
  36. case 25: output = "z"; break;
  37.  
  38. }
  39.  
  40. return output;
  41. }
  42. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.