fork download
  1. import java.util.Random;
  2.  
  3. public class Main {
  4.  
  5. public static void main(String[] args) {
  6. // int x = new java.util.Random().nextInt(N) + M; <従来>
  7. // xの最大値 = M + N - 1
  8. // xの最小値 = M + 0
  9.  
  10. // int x = new java.util.Random().nextInt(N - M + 1) + M; <修正後>
  11.  
  12. final int M = 2; // 最小値
  13. final int N = 12; // 最大値
  14. Random rand = new Random();
  15. int x;
  16. for (int i = 0; i < 30; i++) {
  17. x = rand.nextInt(N - M + 1) + M;
  18. System.out.println(x);
  19. }
  20.  
  21. }
  22.  
  23. }
  24.  
Success #stdin #stdout 0.07s 3359744KB
stdin
Standard input is empty
stdout
11
2
5
12
6
10
12
6
2
4
5
2
5
9
11
12
3
4
9
10
12
7
4
2
12
10
10
2
12
4