您的位置:首页 > 编程语言 > Java开发

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

2016-03-07 18:45 387 查看
/**

 * 构造一个长度为N的String,要求String里的字母必须是以下4个“A,G,C,T”并且这4种字母随机排列

 * @author different-wy

 */

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
;

                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;

        }

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息