Generating a random character out of any two in Java

后端 未结 4 1754
太阳男子
太阳男子 2021-01-23 08:32

How do I generate a random character between two specific characters? For e.g; I want to generate either one of \'h\' or \'v\'.

Thanks

4条回答
  •  清歌不尽
    2021-01-23 09:08

    To generate a random character between two given characters you could do the following:

       char genRandChar(char char1, char char2)
        {
           char retVal;
    
           int val1= (int) char1; //cast char1 to its integer ASCII value
           int val2= (int) char2; //cast char2 to its integer ASCII value
    
           //assuming val1

提交回复
热议问题