[SOLVED] Random word generator - Solution inside

Discussion in 'Plugin Development' started by beatcomet, Jul 15, 2011.

Thread Status:
Not open for further replies.
  1. Offline

    beatcomet

    Hi guys, I need some help.
    I'm developing a plugin that uses a radom word generator.
    I managed to make this king of system using switch and 2 variables (String and int)
    Every letter gets a number and by using a random i'm generating a number which is being convered into a String and added to the name.

    Here is the currnt system :
    Code:java
    1.  
    2. Random rand = new Random();
    3. String gname = "";
    4. String iletter = "";
    5. int letter;
    6.  
    7. while (gname.length() < 8){
    8. letter = rand.nextInt(36-1+1)+1;
    9. switch (letter){
    10. case 1:
    11. iletter = "A";
    12. break;
    13. case 2:
    14. iletter = "B";
    15. break;
    16. case 3:
    17. iletter = "C";
    18. break;
    19. case 4:
    20. iletter = "D";
    21. break;
    22. case 5:
    23. iletter = "E";
    24. break;
    25. case 6:
    26. iletter = "F";
    27. break;
    28. case 7:
    29. iletter = "G";
    30. break;
    31. case 8:
    32. iletter = "H";
    33. break;
    34. case 9:
    35. iletter = "I";
    36. break;
    37. case 10:
    38. iletter = "J";
    39. break;
    40. case 11:
    41. iletter = "K";
    42. break;
    43. case 12:
    44. iletter = "L";
    45. break;
    46. case 13:
    47. iletter = "M";
    48. break;
    49. case 14:
    50. iletter = "N";
    51. break;
    52. case 15:
    53. iletter = "O";
    54. break;
    55. case 16:
    56. iletter = "P";
    57. break;
    58. case 17:
    59. iletter = "Q";
    60. break;
    61. case 18:
    62. iletter = "R";
    63. break;
    64. case 19:
    65. iletter = "S";
    66. break;
    67. case 20:
    68. iletter = "T";
    69. break;
    70. case 21:
    71. iletter = "U";
    72. break;
    73. case 22:
    74. iletter = "V";
    75. break;
    76. case 23:
    77. iletter = "W";
    78. break;
    79. case 24:
    80. iletter = "X";
    81. break;
    82. case 25:
    83. iletter = "Y";
    84. break;
    85. case 26:
    86. iletter = "Z";
    87. break;
    88. case 27:
    89. iletter = "1";
    90. break;
    91. case 28:
    92. iletter = "2";
    93. break;
    94. case 29:
    95. iletter = "3";
    96. break;
    97. case 30:
    98. iletter = "4";
    99. break;
    100. case 31:
    101. iletter = "5";
    102. break;
    103. case 32:
    104. iletter = "6";
    105. break;
    106. case 33:
    107. iletter = "7";
    108. break;
    109. case 34:
    110. iletter = "8";
    111. break;
    112. case 35:
    113. iletter = "9";
    114. break;
    115. case 36:
    116. iletter = "-";
    117. break;
    118. }
    119. gname = gname + iletter;
    120. }
    121. }
    122.  


    As you can see, i'm using a while loop and then generating a random number that is being added to the word, at last you get a word made out of random letters and numbers.

    Is there a way to make it shorter?
    If there is pleas teach me how :)

    Solved!!
    This code is way shorter than the old one!!
    tested it using System.out.println and it generates a random word.
    Code:java
    1.  
    2. int maxlength = 8;
    3. final String chars = "abcdefghijklmnopqrstuvwxyz123456789-";
    4. String word = "";
    5. while(word.length() < maxlength){
    6. Random rand = new Random();
    7. int x = rand.nextInt(36 - 1 + 1) +1 ;
    8. word = word + chars.charAt(x);
    9. System.out.println(word.toUpperCase());
    10. }
    11.  


    what did I do ?
    maxlength : is the length of the generated word
    String word : the word, it's empty at the beggining but this function adds chars to it!
    while loop : used to check if the word is at the right length, comparing it to the maxlength.
    x : used as a variable, The rundom bumber generator generates a number and the number will be used to get one of the chars from the String chars, ant put it in the new word.
     
  2. Offline

    Shamebot

    If you want to generate a random seed, you simply can use a random long.
     
  3. Offline

    beatcomet

    not a random seed but a code (for the redeemcodes plugin I made), as you can see the old method was long and long. So I managed to make it a lot shorter and now i'm happy :)
     
  4. Offline

    Shamebot

    ah i misread the title, it's word not world. You could also cast a random byte to a char, but there would be special characters in it.
     
  5. Offline

    beatcomet

    Yea I know :)
    The only thing matters is that it's working and it's shorter than the mega code I use at first ;)
     
Thread Status:
Not open for further replies.

Share This Page