I'm trying to create an android application which will generate random series of values (integer numbers in this case) in a given range (but NOT equal between them) and display them in a simple TextView
Let's say we have the range R = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
Each time I press the button "Generate" I want to randomly generate 5 different results
Example for each "Generate":
- 4, 9, 2, 12, 10
- 5, 1, 6, 8, 13
- 10, 4, 6, 8, 2
- etc...
EDIT (works now) Thanks for all the help!
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);
}
}
hope use full to you....
If I understand this correctly, you all the numbers should be unique. You could fill a list with the range you want to draw from. Every time you have selected a value from it, you should remove it from the list so it won't be selected a second time. I hope this description is clear enough. If not, I will provide some code.
data contains your range numbers.
set this text in your TextView.
in the edit code:
needs to change to:
because you want to choose a random index from your data.