Generating a random character out of any two in Java

后端 未结 4 1753
太阳男子
太阳男子 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:05

    how about:

    import java.util.Random;
    
    
    ....
    
    Random randomGenerator = new Random();
    int randomInt = randomGenerator.nextInt(100);
    char chosen = randomInt % 2 == 0 ? 'h' : 'v';
    

提交回复
热议问题