import java.util.Random;
public class Generator extends javax.swing.JFrame {
String[] gamesL = new String[] {"Dota 2", "Garrys mod", "Dungeon Defenders"};
Random rand = new Random();
public Generator() {
initComponents();
}
private void initComponents() {...}
private void generateActionPerformed(java.awt.event.ActionEvent evt) {
display.setText("You shall play " + gamesL[0]);
}
Asked
Active
Viewed 98 times
-4

JasonMArcher
- 14,195
- 22
- 56
- 52
-
1Possible duplicate of http://stackoverflow.com/questions/363681/generating-random-numbers-in-a-range-with-java – Ali Hashemi Oct 13 '13 at 14:13
-
From the code snippet, it appears OP wants to shuffle the array and then choose the first element. But the "possible duplicate" leads to the better solution of choosing a random number from `{0,1,2}`, and then taking that element from the array. – Teepeemm Oct 13 '13 at 14:45
2 Answers
3
use Collections.shuffle
(your arraylist) to get the shuffled random array;
you need to import java.util.Collections.

ohmygirl
- 239
- 1
- 5
0
This unsorts an (String) array (vs. Collection)
public void unsortStringArray(String[] a) {
int len = a.length;
for(int sourceIdx = 0; sourceIdx<len; sourceIdx++) {
int destIdx = (int) Math.floor(Math.random() * len);
String sx = a[destIdx];
a[destIdx] = a[sourceIdx];
a[sourceIdx] = sx;
}
}

Michael Besteck
- 2,415
- 18
- 10