views:

129

answers:

5

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...

Any help is appreciated! Thank you in advance.

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);
  }
}
+1  A: 
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]);
}

data contains your range numbers.

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

set this text in your TextView.

mohammad shamsi
You have a slight error: it should be r.nextInt(lengthOfData) + rangeMin
I82Much
@182Much thanks, edited.
mohammad shamsi
Thank you for your answer, please forgive my newbieness but how could I join this in an android activity and print it to a TextView? I seem to get it wrong and the application forces to close before it even starts.
triump
@triump: edited
mohammad shamsi
Thank you again for your quick response! I really appreciate your help. I have edited my original post, showing what I've done. There must be something wrong still.
triump
A: 

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.

Maurits Rijk
A: 
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)
};
I82Much
A: 
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;
}
instanceofTom
+1  A: 

in the edit code:

int idx = r.nextInt();

needs to change to:

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

because you want to choose a random index from your data.

mohammad shamsi
That was the part that was missing! Thank you!
triump