本帖最后由 JDXjudy 于 2010-06-16 15:31:04 编辑

解决方案 »

  1.   

    我知道先可以
     int a[]={1,2,3,4,,5,6,7,8};
    boolean b[]=new boolean[a.length];
    再用math.radom随取机
    不过自己也很晕  求高手详解
      

  2.   


    List   list   =   new   ArrayList(); 
              java.util.Random   random   =   new   java.util.Random();           while   (list.size()   <   177)   { 
                  Integer   ii   =   new   Integer(random.nextInt(177)); 
                  if   (!list.contains(ii))   { 
                      list.add(ii); 
                  } 
                  System.out.println( "----- "   +   list.toString()); 
            }
      

  3.   

    奇怪,要多种思路,又限制使用boolean方法?为什么?--
    我倒觉得,使用Math.Random()。随机出一个数组对应的数来后。以后直接根据随机数去取值。
      

  4.   


    package net.csdn.test;import java.util.ArrayList;
    import java.util.List;
    import java.util.Random;public class Test {
    public static void main(String[] args) {
    int[] arrays = {1,3,2,34543,2131,23,234,25,453,6234,24,543,531,3134,347,67,452};
    Random ran = new Random();
    int count = ran.nextInt(arrays.length),i = 0;
    List<Integer> list = new ArrayList<Integer>();
    while(i < count) {
    Integer tmp = arrays[ran.nextInt(arrays.length)];
    if(list.contains(tmp))
    continue;
    else
    list.add(tmp);
    i++;
    }
    System.out.println(list);
    }
    }还有Set是去重复元素的,如果有的不要求集合内的元素一定要与循环次数相等的话,可以使用Set。
      

  5.   

    以前有遇到过,有一个不错的思路用Math.Random产生数组下标,把对应元素输出,再把这个位置设置为数组末尾的元素,.    public static void Select(int count)//count表示要从数组中选多少个元素
        {
           int[] num = {1,2,3,4,5,6,7,8,9,10};
           int length = num.length;
           for (int i = 0; i < count; i++)
           {
            int index = (int)(Math.random() * length);//产生下标
            System.out.print(" " + num[index]);
            num[index] = num[length - 1];
            length--;
           }  
        }
      

  6.   

    这个问题值得弄明白,以后会经常用的,
    来晚了,代码不写了
    提醒:nextInt(int n) 
              返回一个伪随机数,它是取自此随机数生成器序列的、在 0(包括)和指定值(不包括)之间均匀分布的 int 值。
    不包括右值; Math.random()有0没1;
    int为截位取整。有的已经把最后的元素丢了
      

  7.   


    import java.util.Random;
    public class Test {

    private static int[] arr;
    private static int index;
    private static Random ran;

    public Test(int[] arr){
    this.arr = arr;
    this.index = arr.length - 1;
    this.ran = new Random();
    }

    public static void main(String[] args) {
    int[] nums = {1,4,6,13,83,91,8,31,7,14,78,20,65};
    Test test = new Test(nums);
    for(int i = 0; i < nums.length; i++){
    System.out.println(test.getRaNum());
    }
    }

    public int getRaNum(){
    if(index == 0)
    return arr[0];
    int temp = ran.nextInt(index);
    arr[temp] ^= arr[index];
    arr[index] ^= arr[temp];
    arr[temp] ^= arr[index];
    index--;
    return arr[index+1];
    }
    }
      

  8.   

    上边的没进行异常处理  重写了个import java.util.Random;
    public class Test {

    private static int[] arr;
    private static int index;
    private static Random ran;

    public Test(int[] arr){
    this.arr = arr;
    this.index = arr.length - 1;
    this.ran = new Random();
    }

    public static void main(String[] args) {
    int[] nums = {1,4,6,13,83,91,8,31,7,14,78,20,65};
    Test test = new Test(nums);
    for(int i = 0; i < 2 * nums.length; i++){
    System.out.println(test.getRaNum());
    }
    }

    public int getRaNum(){
    if(index == 0){ //如果数组的数都随机取了一次了,就重新对整个数组随机取数;
    index = arr.length - 1;
    return arr[0];
    }
    int temp = ran.nextInt(index);
    arr[temp] ^= arr[index];
    arr[index] ^= arr[temp];
    arr[temp] ^= arr[index];
    index--;
    return arr[index+1];
    }
    }
      

  9.   

    //Runnable接口实现多线程
    public class Random implements Runnable {
           String name[]={"a","b","c","d","e","f","g"};
           int numbers[]= new int[3];
           int j=0;
           int number;
    public void run()
    {
    boolean fleg;
    // TODO Auto-generated method stub
    do
    {
    fleg=false;
    number = (int)(Math.random()*7);
    for(int i =0;i<numbers.length;i++)
    {
    if(number==numbers[i])
    {
    fleg=true;
    break;
    }
    }
    }while(fleg);
    numbers[j]=number;
    System.out.println("第"+(j+1)+"的数为:"+name[numbers[j]]);
    j++;
    }
    }
    //测试
    public class RandomTest {
    public static void main(String s[])
    {
    Random ran = new Random();
    Runnable able = new Random();
    new Thread (able).start();
    new Thread (able).start();
    new Thread (able).start();

    }}
      

  10.   

    呵,楼主跟我一样,刚学JAVA。数组好办,新建一个就OK,然后随机数,FOR上几次,取几次,但一想到要排除已经取过值的,好象就没办法了,刚开始的想法是,取出一个数,然后就从该数组中把这个删除,查了API发现,数组长度一旦确定,就无法再改变其length,发现还有一个容器Arraylist,应该可以实现,但还没有看到这里,所以暂时放下了。
      

  11.   

    wo ye shi  chuxue  zhe ...     bu cuo de  code   
      

  12.   

    这个算法,如果在选择的元素个数与原数组的个数相差不多时,list.contains(ii)的概率会比较高,这样可能在取最后几个数时,会发生多次随机。我比较赞同5楼的算法
      

  13.   

    如果数组可以改动的话,一般都用5楼的方法如果不改动数组,就new个一样长boolean[],将访问过的下标在boolean[]里面置为true.