2010-08-27 75 views
4

我試圖創建一個Android應用程序,它將在給定範圍內產生隨機系列的值(在這種情況下是整數)(但它們之間有不等於)並顯示他們在一個簡單的TextView如何從給定範圍內選擇隨機值

假設我們具有範圍R = [1,2,3,4,5,6,7,8,9,10,11,12,13]

每次按下按鈕「生成」我想隨機生成5個不同的結果

實施例對每個 「生成」:

  • 4,9,2,12,10
  • 5,1,6,8,13
  • 10,圖4,6,8 ,2
  • 等...

EDIT(現在的工作)的所有幫助謝謝!

public class random extends Activity { 


static final Integer[] data = new Integer[] { 
    1, 2, 3, 4, 5, 6, 7, 8 
    }; 


@Override 
public void onCreate(Bundle icicle) { 
    super.onCreate(icicle); 
    setContentView(R.layout.main); 

    Random r = new Random(); 

    Set<Integer> mySet = new HashSet<Integer>(); 
    while (mySet.size() < 5) { 
     int idx = r.nextInt(data.length); 
     mySet.add(data[idx]); 
    } 

    String text = ""; 
    for (Integer i : mySet) { 
     text = text + i + ", "; 
    } 
     TextView Numbers = (TextView)findViewById(R.id.shownumbers); 
     Numbers.setText(text); 
    } 
} 
+0

在Clojure中:(坐5(洗牌(1範圍14))) 現在比較,所提出的Java解決方案在這裏;)太糟糕的Clojure目前在Android上非常緩慢。 – 2010-08-27 18:15:50

回答

2
Random r = new Random(<a seed number>); 

Set<Integer> mySet = new HashSet<Integer>(); 
while (mySet.size() < 5) { 
    int idx = r.nextInt(<length of your data>) 
    mySet.add(data[idx]); 
} 

數據中包含的數字範圍。

String text = ""; 
for (Integer i : mySet) { 
    text = text + i + ", "; 
} 

在TextView中設置此文本。

+0

謝謝你的回答,請原諒我的新手,但我怎麼能加入這個android活動並將其打印到TextView?我似乎弄錯了,應用程序在它開始之前就會關閉它。 – thpoul 2010-08-27 22:45:40

+0

再次感謝您的快速響應!我非常感謝你的幫助。我編輯了我原來的帖子,展示了我所做的。還有一些問題。 – thpoul 2010-08-27 23:41:32

0

如果我正確理解這一點,你所有的數字應該是唯一的。你可以用你想要繪製的範圍來填充一個列表。每次從中選擇一個值時,都應該從列表中刪除它,這樣它就不會再被選中。我希望這個描述足夠清楚。如果不是,我會提供一些代碼。

0
int low = ...; 
int high = ...; 
List<Integer> choices = new LinkedList<Integer>(); 
for (int i = low; i <= high; i++) { 
    choices.add(i); 
} 

Collections.shuffle(choices); 

int[] choices = new int[] { 
    choices.get(0), 
    choices.get(1), 
    choices.get(2), 
    choices.get(3), 
    choices.get(4) 
}; 
0
final StringBuilder builder = new StringBuilder(); 
final Random r = new Random(System.currentTimeMillis()); 
final Set<Integer> numbers = new HashSet<Integer>(); 

public String getRandomNumbers(int count,int min, int max) 
{ 
    if(count > (max - min) || (max < min)) 
     throw new IllegalArgumentException("There is an error with the parameters provided"); 

    builder.setLength(0); //Clear StringBuilder 
    numbers.clear(); //Clear the number list 

    int i=0; 
    while(i < count) 
    { 
     int aRandomNumber = (r.nextInt() % max) +min; 

     if(numbers.contains(aRandomNumber)) // If we have seen this number already 
      continue; 
     else 
     { 
      i++; 
      numbers.add(aRandomNumber); 
      builder.append(aRandomNumber); //Add number to string 

      if(i < (count-1)) 
       builder.append(", "); // Add a comma if it's not the last number 
     } 
    } 

    String output = builder.toString(); 
    builder.setLength(0); //A polite clearing 
    numbers.clear(); 

    return output; 
} 
2
在編輯代碼

int idx = r.nextInt(); 

需要改變到:

int idx = r.nextInt(data.length); 

,因爲你要選擇從您的數據的隨機指數。

+0

這是失蹤的部分!謝謝! – thpoul 2010-08-28 14:05:29

0
public int[] generateSeries(){ 
int[] R = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13}; 
    int [] newarray=new int[5]; 
Collections.shuffle(Arrays.asList(R)); 
for(int i=0;i<5;i++) 
{ 
    newarray[i]=R[i]; 
    System.out.println("values => "+R[i]); 
} 
return newarray; 

希望使用全給你....

相關問題