Java - generate Random range of specific numbers w

2019-01-04 04:06发布

Sounds simple enough...but I've been plugging away at this, trying to find the one and all solution.

For a range of numbers, say 1-12, I want to generate a random sequence within that range, and include 1 and 12.

I don't want duplicate numbers though.

So I would want something like this - 3,1,8,6,5,4 ..and so on, every number from 1-12.

Then I want to put these random numbers into an Array and use that array to 'randomly' select and display some items (like inventory pulled from database) on a jsp page.

The problem with what I've tried thus far, is that there are a lot of duplicate numbers being generated...or, not ALL of the numbers are chosen.

Is there a simple solution to this problem?


Edit

Test#1 using Collections and shuffle() method -

ArrayList<Integer> list = new ArrayList<Integer>(10);
for(int i = 0; i < 10; i++)
{
  list.add(i);
}
Collections.shuffle(list);

String[] randomNumbers = (String[])list.toArray();

for(int i = 0; i < 10; i++)
{
  out.print(randomNumbers[i]+"<br>");
}

The result was a sequence with duplicate values -
chose = 3
chose = 8
chose = 7
chose = 5
chose = 1
chose = 4
chose = 6
chose = 4
chose = 7
chose = 12

Test #2 - using Random math class

int max = 12;
int min = 1;

int randomNumber = 0;

String str_randomNumber = "";

for(int i=0; i<10; i++) {
    //int choice = 1 + Math.abs(rand.nextInt(11));
    int choice = min + (int)(Math.random() * ((max - min) + 1));

    out.print("chose = "+choice+"<br>");
}

The result was just like using Collections.shuffle().

6条回答
Animai°情兽
2楼-- · 2019-01-04 04:25

You could just put all the numbers you want in a List and then order the List randomly and then convert the randomly ordered list to an array, e.g.

List<Integer> list = new ArrayList<Integer>();

for (int i = 1; i <= 12; i++) {
    list.add(i);
}

Collections.sort(list, new Comparator<Integer>() {

    @Override
    public int compare(Integer o1, Integer o2) {
          return Math.random() > 0.5 ? 1 : -1;
    }
);
Integer[] array = list.toArray(new Integer[list.size()]);
查看更多
孤傲高冷的网名
3楼-- · 2019-01-04 04:32

If you are using MySQL or SQLLite as your database you can do this randomization at the SELECT query level by using ORDER BY RAND() for restricting to 1-12 you can put a where clause WHERE ID >=1 AND ID <=12 ORDER BY RAND()

查看更多
孤傲高冷的网名
4楼-- · 2019-01-04 04:33

You can put all numbers from 1 to 12 in order into array and then use some shuffling algorithm to randomize the order of them e.g. http://www.leepoint.net/notes-java/algorithms/random/random-shuffling.html.

查看更多
狗以群分
5楼-- · 2019-01-04 04:35

You can fill an array with all values from 1 to 12 and then shuffle them (see e.g. Why does Collections.shuffle() fail for my array?)

查看更多
beautiful°
6楼-- · 2019-01-04 04:37

Random number generation allows for duplications. If you want a range of random numbers without duplication, I suggest the following:

  1. Generate a random number (I will refer to this a numberX).
  2. Add to a Set object.
  3. Check the size of the Set object, if it is the desired size, you are done. If it is smaller than the desired size, goto step 1
查看更多
疯言疯语
7楼-- · 2019-01-04 04:42

This is a utility method for creating a random Integer number :

public static int randomInteger(int min, int max) {
    Random rd = new Random();
    return rd.nextInt((max - min) + 1) + min;
}

This is an algorithm that always produces a unique Set of integers:

public static Set<Integer> makeRandomSet(int howManyNumber, int startNumber, int endNumber){
    Set<Integer> integerSet = new HashSet<>();

    boolean couldBeAdded = false;
    for(int i=0; i< howManyNumber; i++) {
        while (!couldBeAdded) {
            Integer randomInt = randomInteger(startNumber, endNumber);
            couldBeAdded = integerSet.add(randomInt);
        }

        couldBeAdded = false;
    }

    return integerSet;
}

We made use of add method return type to check the duplicate value within our Set.

And here is the test code:

public static void main(String[] args) {
    Set<Integer> randomSet = makeRandomSet(6, 1, 54);
    System.out.println(randomSet);
}

The output of the above code is 6 random unique integers number between 1 and 54

查看更多
登录 后发表回答