要怎么做。比如 我要输出20以内的 7个随机数。 但是 要求 都不重复。。谢谢

解决方案 »

  1.   

    who can help me ~~~?
      

  2.   

    可以在设置一个flag[20]作为标志位,初始为false,一旦此随机数被使用过,改为true。然后,根据标志位,如果是已经有的,就重新获得随机数。
      

  3.   

    public class testCSDN {    /**
         * @param args
         */
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            int[] a = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20};
            int loc = 0;
            for (int i = 0; i < 7; i ++){           
                loc = (int)(Math.random()*19); 
                int temp = a[i];
                a[i] = a[loc];
                a[loc] = temp;            
            }
            for (int i = 0; i < 7; i ++){
                System.out.print(a[i] + "  ");
            }        
        }
    }
      

  4.   

    public class testCSDN {    /**
         * @param args
         */
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            int[] a = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20};
            int loc = 0;
            for (int i = 0; i < 7; i ++){           
                loc = (int)(Math.random()*19); 
                int temp = a[i];
                a[i] = a[loc];
                a[loc] = temp;            
            }
            for (int i = 0; i < 7; i ++){
                System.out.print(a[i] + "  ");
            }        
        }
    }
    楼主可以试试
      

  5.   

    思路就是你生成的随机数放在一个数组中,然后做比较,一但是重复的就重新生成,当然保存在map中也可以,主要就是这个思路
      

  6.   


    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.List;
    import java.util.Random;
    public class Num {
        public static void main(String[] args) {
            List<Integer> list = new ArrayList<Integer>();
            //初始化list
            for(int i=1; i<20; i++) {
                list.add(i);
            }
            
            int[] array = new int[7];
            Random random = new Random();
            for(int i=0; i<array.length; i++) {
                array[i] = list.remove(random.nextInt(list.size()));
            }
            
            System.out.println("产生的不重复的随机数:");
            System.out.println(Arrays.toString(array));
        }
    }
      

  7.   


    import java.util.*;public class TestRandom {
        public static void main(String[] args) {
    Set<Integer> integers = new HashSet<Integer>();
    Random random = new Random(47);
    while (integers.size() < 7)
        integers.add(random.nextInt(20) + 1);
    System.out.println(integers);    }
    }
      

  8.   

    在List里面放1-20吧 然后用int i = list.remove(int index);
    这样应该能保证不重复吧
      

  9.   

    Random,放到set集合中,再判断下就OK了
      

  10.   

    用TreeSet 
    import java.util.Collection;
    import java.util.Random;
    import java.util.TreeSet;public class DistinctNum {
    public static Collection<Integer> getDistinctNum(int count) { //count产生的数据个数
    Collection<Integer> set = new TreeSet<Integer>();
    Random rand = new Random();
    while(set.size()<count){
    set.add(rand.nextInt(100));
    }
    return set; }}
    试试
      

  11.   

    参考一下:import java.util.ArrayList;
    import java.util.List;
    import java.util.Random;public class RandomTest {
    public static void main(String[] args) {
    Random random = new Random();
    List<Integer> list = new ArrayList<Integer>();
    int num = random.nextInt(20) + 1;
    while (list.size() < 7) {
    if (list.contains(num))
    continue;
    list.add(num);
    num = random.nextInt(20) + 1;
    }
    for (int i : list) 
    System.out.print(i + " "); }
    }
      

  12.   

    也可利用种子:
    Random(long seed) 
              Creates a new random number generator using a single long seed.由于不同的种子,随机数序列不同,例用1作种子: 
    Random ran = new Random(1);
    用当前的系统时间作为种子 
    Random ran= new Random((new Date()).getTime());只要种子不同,随机数不会同,这样少了一次判断。
      

  13.   

    看来我错了,要保证一定不重复,要有相应措施,如用HashSet。
    import java.util.*;
    public class Test{
       public static void main(String args[]){
       Set<Integer> rand = new HashSet<Integer>();
       Random random = new Random();   do rand.add(random.nextInt(20) + 1);
       while (rand.size() < 7);
            
       System.out.println(rand);
      }
    }
      

  14.   

    用List的shuffle也行:
    import java.util.*;
    public class Test{
        public static void main(String[] args){
    List<Integer> list = new ArrayList<Integer>();
    for(int i=1; i<20; i++) {
    list.add(i);
    }
    Collections.shuffle(list);
    for(int i=0; i<7; i++)
    System.out.print(list.get(i)+" ");
        }
    }
      

  15.   

    其实这种在N个元素中随机找出M个的情况可以分开两种:1、如果N远远比M大,使用HashSet等方式判断是否有重复,直到找出M个不重复为止2、如果M比较接近于N,则应该对数组N使用Collections.shuffle这样的方式,然后找前M个
    方式2可以保证运算次数固定,不存在运气好与不好的问题
      

  16.   


    #include<iostream>
    #include<cstdlib>
    using namespace std;
    void main()
    {
      int s[7]={0};
      int x = rand()%20;
      int n=0;
      for(int i=0;n<7;i=0)
      {
        while(i< n&&x != s[i])
          i++;
        if(i==n)
        { 
          s[n]=x;
          ++n;
          x= rand()%20;
        }
      }
      for(int j=0;j<7;j++)
      {
        cout<<s[j]<<"\t";
      }
      cout<<endl;
    }
      

  17.   

    说出了我想说的,刚好我前面写了这两种方式的代码。不少人说9楼的好,其实他用了一list,一array,每次还要remove,效率、开销还不如只用一HashSet的。
      

  18.   

    第一次,在0到19中roll随机数,把它当作数组下标从数组中取数,然后把数组中第20个元素换位置。
    第二次,在0到18中roll随机数,把它当作数组下标从数组中取数,然后把数组中第19个元素换位置。
    .....
    不但不重复,而且只roll7次,只是加了两个自减,一个交换就OK了。
      

  19.   

    你不知到Arraylist里面remove一个后面所有的数要重新往前移一个
    开销不是更大 又不是linklist 
      

  20.   

    如果使用List就直接使用Collections.shuffle就行了,然后取前N个。这样效率才高如果是数组也是一样参考里面的实现:int N = 20;
    int[] list = new int[N];
    for (int i = 1; i <= N; i++)
    list[i - 1] = i;
    Random rnd = new Random();
    for (int i = N; i > 1; i--) {
    int j = rnd.nextInt(i);
    int tmp = list[j];
    list[j] = list[i - 1];
    list[i - 1] = tmp;
    }
    System.out.println(Arrays.toString(list));
      

  21.   

    import java.util.*;public class TestRandom {
    public static void main(String[] args) {
    HashSet<Integer> hs = new HashSet<Integer>();
    while(hs.size() < 7) {
    hs.add((int)(Math.random() * 20));
    }
    for(Iterator it = hs.iterator(); it.hasNext();) {
    System.out.println(it.next());
    }
    }
    }
    跟10楼差不多
      

  22.   

    续上,根据该方法写的代码:package net.csdn;import java.util.Random;public class RandomSerial {

    public static final Random RANDOM_CREATOR = new Random( System.currentTimeMillis() );

    /**
     * Create an random integer serial, in which all element is different from each other.
     * @param range Range of the serial, that means every element of the serial is between 1 and serial
     * @param length Length of the seiral
     * @return The array format of the created serial
     * @author xiaoqiao_82
     */
    public static int[] getSerial( int range, int length ) {

    if( range<1 || length>range ) {
    throw new IllegalArgumentException( "Cannot create serial of range " + range + 
    " and length " + length );
    } int[] source = new int[range];
    for( int i=0; i<source.length; i++ ) {
    source[i] = i;
    }
    int [] serial = new int[length];
    int num;
    int max;

    for( int i=0; i<length; i++ ) {
    max = range - i;
    num = RANDOM_CREATOR.nextInt( max );
    serial[i] = source[num] + 1;
    source[num] = source[max-1];
    }

    return serial;
    }

    public static void printSerial( int[] serial ) {
    if( null==serial || 0==serial.length ) {
    System.out.println( "The serial is empty!" );
    } else {
    System.out.print( "The serial: " );
    for( int i=0; i<serial.length-1; i++ ) {
    System.out.print( serial[i] + ", " );
    }
    System.out.println( serial[serial.length-1] );
    }
    }

    public static void main(String [] args) {

    for( int i=0; i<10; i++ ) {
    printSerial( getSerial(20, 7) );
    }

    System.out.println( "--------------------------------" );

    for( int i=0; i<10; i++ ) {
    printSerial( getSerial(4, 4) );
    }

    }}
    运行结果:
    The serial: 19, 10, 1, 17, 20, 8, 4
    The serial: 16, 3, 13, 2, 19, 1, 6
    The serial: 4, 8, 15, 7, 1, 11, 20
    The serial: 14, 4, 18, 17, 15, 13, 11
    The serial: 2, 3, 11, 12, 19, 9, 20
    The serial: 12, 17, 3, 7, 20, 16, 19
    The serial: 10, 8, 7, 5, 11, 15, 6
    The serial: 5, 2, 4, 17, 8, 3, 20
    The serial: 14, 10, 9, 19, 17, 20, 7
    The serial: 15, 14, 11, 7, 3, 16, 9
    --------------------------------
    The serial: 1, 4, 3, 2
    The serial: 4, 1, 3, 2
    The serial: 4, 1, 3, 2
    The serial: 1, 4, 2, 3
    The serial: 3, 1, 4, 2
    The serial: 4, 3, 2, 1
    The serial: 2, 3, 4, 1
    The serial: 4, 2, 3, 1
    The serial: 3, 1, 4, 2
    The serial: 4, 1, 3, 2