问题好像不少,
1.程序生成随机数的功能无法实现你所希望的;
2.每次都是1-10,那是因为HashSet内部其实是调用HashMap实现的,HashSet里面的value作为HasHMap的key,
HashMap默认容量是16,1-10的随机数对16取模来决定数组下标,当然是按着顺序1-10;

解决方案 »

  1.   

    改了一下,换成List来实现你想要的功能:import java.util.ArrayList;
    import java.util.List;public class Demo {

    private static List<Integer> list=new ArrayList<Integer>();

        //定义生成随机数列的方法
      public static void randomSet(int min,int max,int range){
          int temp = (int)(Math.random()*(max-min+1)+min);
          if(!list.contains(temp))
          {
           list.add(temp);  
          }
          
          if(list.size()<range){
              randomSet(min,max,range);
          }
      }
         
      public static void main(String[] args) {
          randomSet(1,10,10);
        
          System.out.println("==========================================");
          
          for(int temp:list)
          {
           System.out.println(temp);
          }              
      }}
      

  2.   

    你调用的那个方法做测试毫无意义,因为HashSet本身就是无序的,你应该换成List去测试。
    HashSet是按Hash算法通过HashCode去排序的,你输入的1-10的哈希算法顺序刚好是自然数顺序才会让你觉得它排序了,你放100以上的数试试就知道了。
      

  3.   

    你这个和hash set 没什么随机 的关系。Math.random()*(max-min+1)+min
    你已经限制范围了。