Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Hi well i have a string array with like 50 strings and i want it to select only 10 randon results and display them on a table layout i get everything right except it only shows one result. heres my code:

private String[] list;
private static final Random rgenerator = new Random();


@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.layout1);

Resources res = getResources();
    list = res.getStringArray(R.array.names);
    String q = list[rgenerator.nextInt(list.length)];
int total = 10;

    for (int current = 0; current < total; current++) {
        // Create a TableRow and give it an ID
        TableRow tr = new TableRow(this);
        tr.setId(100 + current);
        tr.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT));

        // Create a TextView to house the name of the province
        TextView labelTV = new TextView(this);
        labelTV.setId(200 + current);
        labelTV.setText(q);
        labelTV.setTextSize(14);
        labelTV.setGravity(Gravity.CENTER);
        labelTV.setTextColor(Color.WHITE);
        labelTV.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT));
        tr.addView(labelTV);

        tablelayout.addView(tr, new TableLayout.LayoutParams(
                LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
}

whats wrong?

share|improve this question

2 Answers

Quite simple by using a set, that will take care about avoiding duplicates:

Set<Integer> uniques = new HashSet<Integer>();

while (uniques.size() < 10)
  uniques.add(rgenerator.nextInt(list.length);

for (Integer i : uniques)
{
  String q = list[i];

  ..
}
share|improve this answer
so in labelTV.setText(); do i put q? – SUPLMMM Jun 5 '12 at 3:01
You'll want to concatenate all of the Strings in list into a variable, say, text, and then call labelTV.setText(text); – Zéychin Jun 5 '12 at 4:07

This is called "random sampling". Two common techniques are:

  • simply shuffle the entire list/array, making sure you use a fair shuffling algorithm such as that offered by Collections.shuffle(), then take the first n elements;
  • go through the list, each time deciding whether to include the next item based on whether a random number exceeds a threshold representing number of items still needed/number of items left.

If you go for the first method, just make sure that your shuffle is genuinely a fair algorithm. Many programmers if asked to write a shuffle algorithm from scratch get it wrong. Java's Collections.shuffle() is an example of how to do it.

You may also be interested in an article about random sampling in Java that I wrote a while ago which includes example skeleton code for the second case.

For the sake of knowledge, you may also wish to research something called "reservoir sampling", although it is overkill for selecting 10 items from a possible 50.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.