码迷,mamicode.com
首页 > 编程语言 > 详细

Java 在一个数轴上的多个连续区间内产生随机数

时间:2014-09-01 12:04:52      阅读:260      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   os   io   java   ar   for   art   

 1 public class Randomer {
 2 
 3     public static void main(String[] args) {
 4         Random rand = new Random();
 5         System.out.println(rand.nextInt(3)+1);
 6         
 7         int[] a = {1, 1, 1, 1, 4, 4, 4, 5, 5, 7, 7, 7, 9};
 8         int start = 0;
 9         int position;
10         for (int i = 0; i < a.length; i++) {
11             if (a[i] != a[start]) {
12                 position = getRandomBetweenAB(start, i);
13                 System.out.println(position + " : " + a[position]);
14                 start = i;
15             }
16         }
17         position = getRandomBetweenAB(start, a.length);
18         System.out.println(position + " : " + a[position]);
19     }
20     
21     private static int getRandomBetweenAB(int a, int b) {
22         Random rand = new Random();
23         return (rand.nextInt(b - a) + a);
24     }
25 }

 

Java 在一个数轴上的多个连续区间内产生随机数

标签:style   blog   color   os   io   java   ar   for   art   

原文地址:http://www.cnblogs.com/Stone-sqrt3/p/3948788.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!