In Java how do you randomly select a letter (a-z)?

后端 未结 9 1370
北海茫月
北海茫月 2021-01-04 03:50

If I want to randomly select a letter between a and z, I assume I have to use the Random class:

Random rand = new Random();

Bu

相关标签:
9条回答
  • 2021-01-04 04:29

    alter version of @Michael Barker

        Random r = new Random();
        int c = r.nextInt(26) + (byte)'a';
        System.out.println((char)c);
    
    0 讨论(0)
  • 2021-01-04 04:34
    Random r = new Random();
    char c = (char) (r.nextInt(26) + 'a');
    
    0 讨论(0)
  • 2021-01-04 04:35
    import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic;
    ...
    randomAlphabetic(1).toLowerCase()
    

    this gives you a string with single character

    0 讨论(0)
提交回复
热议问题