天天看点

【java】Math.random()的范围设置

public class RandomArray {         public static void main(String[] args) {                 String result = agct(5);                 System.out.println(result);         }         public static String agct(int n){                 char[] arr = new char[n];                 int t = 0;                 for(int i=0;i<arr.length;i++){                         for(;;){                                 //问题:怎样控制随机数的范围?                                 //百度得出:随机数在 a与b之间 (a小b大 为正数) Math.random*(b-a+1)+a                                t = (char)(Math.random()*(90-65+1)+65);                                if(t==65||t==71||t==67||t==84){                                        break;                                }                         }                         arr[i] = (char)t;                 }                 String result = new String(arr, 0, arr.length);                 return result;         } }