1

I require help with a small Java problem I am having. I have an array called lowest. An example entry of lowest would be [6 2 9 2 7 2 3].

What I need to do is choose the lowest number from the array, and if there are two or more entries which are the lowest and identical, I need to choose one randomly. So in this case, since 2 is the lowest, I would have to choose either entry 1, 3 or 5 randomly.

How do I do this?

2
  • This looks like a typical class homework problem. :) Commented Apr 9, 2011 at 13:51
  • 1
    What I need to do is choose the lowest number from the array - no, you need to choose an index, of the lowest number. Commented Apr 9, 2011 at 13:54

5 Answers 5

4

Find the indexes in a loop and put them in a collection (e.g. ArrayList lowest). Then use Collections.shuffle(lowest) and pick the first element lowest.get(0). This should do what you want.

Sign up to request clarification or add additional context in comments.

Comments

1

I would find the indexes, put them in an array, and generate a random number between 0 and array.length-1, returning that index from the new array.

Comments

1
public static void findIndexOfMinimumRandomly(List<Integer> listOfValues) {

    //Find minimum of values
    int minValue = Collections.min(listOfValues);

    //Create a list to hold indexes of Minimums
    List<Integer> indexesOfMins = new ArrayList<Integer>();

    //Fill list with index values
    for (int i = 0; i < listOfValues.size(); i++) {
        if (listOfValues.get(i) == minValue) {
            indexesOfMins.add(i);
        }
    }

    //Generate Random Integer to choose one of the indexes
    int randomInt = new Random().nextInt(indexesOfMins.size());

    //Choose one of the indexes randomly
    int randomIndex = indexesOfMins.get(randomInt);

    System.out.println(randomIndex);

}

Comments

0
int lowestIndex = 0;

//Make sure we start with the biggest number possible
int lowestValue = Integer.MAX_VALUE;  

for(int i = 0; i < array.length; i++){
     if(array[i] < lowestValue){
          lowestValue = array[i];
          lowestIndex = i;
     }
}

Now lowestIndex is the index of the lowest number!

Now this will always return the first of the lowest values. If you want to get a random lowest, you'll have make an array or ArrayList with the indexes of all the elements being the same as the lowest and then choose randomly. It depends on how big this array is and how efficient you want it to be. If efficiency is no big problem, you can do this after the code above:

Random rand = new Random();
ArrayList<Integer> minIndexes = new ArrayList<Integer>();
for(int i = 0; i < array.length; i++){
    if(array[i] == lowestValue){
          minIndexes.add(i);
     }
}

int r = rand.next(minIndexes.size());
int randomIndex = minIndexes.get(r);

Now randomIndex is the index you want!

Comments

0
public static void main(String[] args) {
    Set<Integer> treeSet = new TreeSet<Integer>();
    treeSet.addAll(Arrays.asList(new Integer[]{6,2,9,2,7,2,3}));
    Iterator<Integer> iter = treeSet.iterator();
    if(iter.hasNext())
    {
        //the lowest value from the array
        System.out.println(iter.next());
    }
}

In this way there will be always the lowest value at the start of iterator.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.