2010-12-10 75 views
3

我已經創建了以下方法以創建唯一的隨機數。 (此唯一值屬於樹的節點):創建唯一的隨機數

static Random rand = new Random(); 
public static ArrayList<Node> go(int n) { 
    ArrayList<Node> list = new ArrayList<Node>(); 
    ArrayList<Integer> numList = new ArrayList<Integer>(); 
    // TODO Auto-generated method stub 
    for(int i = 1; i<=5; i++) 
    { 
     int number = rand.nextInt(10)+1; 
     if(list.size()>0 && !check(list,number)) 
     { 
      i--; 
      continue; 
     } 
     numList.add(number); 
     Node node = new Node(); 
     node.data = number; 
     list.add(node); 
    } 
    int w = 0; 
    for (Node d : list) { 
     System.out.println(w+": "+d.data); 
     w++; 
    } 
    return list; 

} 
    private static boolean check(ArrayList<Node> list, int num) { 
    // TODO Auto-generated method stub 
    boolean b = false; 
    /*if(list.size()==0) 
     return true; 
    */ 
    for (Node node : list) { 
     if(node.data == num) 
      b = false; 
     else 
      b = true; 
    } 
    return b; 
} 

但它並不創造獨特的數字和還有我的列表中重複。像:

0: 10 
1: 1 
2: 10 
3: 5 
4: 6 
+2

是否有任何理由不使用[Java SecureRandom](http://download.oracle.com/javase/6/docs/api/java/security/SecureRandom.html)? – birryree 2010-12-10 15:18:32

+3

SecureRandom提供不可預測的序列。 Random有很多非安全性用途,其中可預測性不是問題,並且有一些需要再現相同序列。 – strainer 2010-12-10 15:57:10

回答

6

問題是,如果發現重複的數字,則不會停止檢查函數內部的for循環。循環繼續,b可以變回真。

你應該做的是什麼,例如:

private static boolean check(ArrayList<Node> list, int num) { 
    for (Node node : list) { 
     if(node.data == num) 
      return false; 
    } 
    return true; 
} 
+0

實際上它應該是相反的:在末尾返回'false'和'true' – Jack 2010-12-10 15:24:09

+0

哎呀,我的壞!編輯!謝謝。 – 2010-12-10 15:26:43

+0

感謝您的幫助 – 2010-12-10 15:34:54

1

在你的檢查方法,這看起來有點狡猾:

if (node.data == num) 
    b = false; 
else 
    b = true 

當然,一旦你找到了一個匹配(例如B = FALSE)要回來嗎?否則下一次循環b可能會被設置爲true。爲了簡化一下,如果你想檢查一個項目是否在一個集合中,你可以做list.contains(元素)

0

你的check函數是錯誤的。目前,它只是返回最近的元素是否匹配num。一旦找到匹配項,您想聲明true(例如return true;)。

事實上,你可以做的一切都沒有b。我相信你可以使用listcontain方法。

0

你應該改變你的check方法是這樣的:

private static boolean check(ArrayList<Node> list, int num) 
    { 
    for (Node node : list) 
     if (node.data == num) 
      return false; 

    return true; 
    } 

這樣,你打開名冊,並儘快返回false你會發現一個相等的元素。如果您能夠完成循環而不返回,則不會找到重複項,您可以返回true

1

你「忘記」使用你準備好的numList

此代碼應該很好地工作:

static Random rand = new Random(); 

public static ArrayList<Node> go(int n) { 
    ArrayList<Node> list = new ArrayList<Node>(); 
    ArrayList<Integer> numList = new ArrayList<Integer>(); 

    for (int i = 1; i <= 5; i++) { 
     int number = rand.nextInt(10) + 1; 
     if (numList.contains(number)) { 
      i--; 
      continue; 
     } 
     numList.add(number); 
     Node node = new Node(); 
     node.data = number; 
     list.add(node); 
    } 
    int w = 0; 
    for (Node d : list) { 
     System.out.println(w + ": " + d.data); 
     w++; 
    } 
    return list; 

} 
3

喬恩特勞斯蒂Arason有你的答案,但是......

既然你允許值(整數)的數量有限,而且因爲你不」要同一個選擇不止一次,也許這將更容易shuffle一個允許值的數組。然後,您可以從數組中選取下一個值,而不必擔心每次檢查是否重複。

在你的示例中,選擇一到十之間的五個值,你可以從一個數組{1,2,3,4,5,6,7,8,9,10}開始,並通過一個shuffle來重新排列它,像{3,4,7,1,10,9,5,8,2,6}。從結果數組中取出前五個值,不用擔心重複。

1

爲了說明@ eaj的觀點。

public static List<Node> go(int n) { 
    List<Integer> numbers = new ArrayList<Integer>(); 
    for (int i = 1; i <= 10; i++) numbers.add(i); 
    Collections.shuffle(numbers); 
    List<Node> nodes = new ArrayList<Node>(); 
    for (Integer data : numbers.subList(0, 5)) 
     nodes.add(new Node(data)); // use a constructor for Node. 
    for (int w = 0; w < nodes.size(); w++) 
     System.out.println(w + ": " + nodes.get(w).data); 
    return nodes; 
} 
+0

謝謝! :)我忘記了洗牌是在Collections API中。 – eaj 2010-12-11 23:54:55

0

這是我的解決方案:

import java.util.ArrayList; 
import java.util.Collections; 


public class comboGenerator { 

    public static void main(String[] args) { 


    ArrayList<Integer> $combo = new ArrayList<Integer>();  // init. array list combo for randomization 


     while ($combo.size() < 6) { 
      int rand = (int) (Math.random()*49+1);   // make new random number 1-49 
      if (!$combo.contains(rand)){     // check if we have that number in array list,{ 
      $combo.add(rand);        // if there is no such number then add it to array list 
      Collections.sort($combo);      // sort the array list small >> large 
      } 
     } 

    System.out.println("Random combination " + $combo); 

} 
} 

,你不能得到相同的數字!