简体   繁体   中英

generate random order but with constraint in java

When I put a integer list, how can I generate another random order but with constraint?

For example, I put integer 1, 2, 3, 4 into the collection and when I try to print result like be "1 2 3 4","1 2 4 3","1 3 2 4" ,"2 1 3 4",or "2 1 4 3"(1 must before 3, 2 must before 4)

thanks in advance

One thing you can consider is swapping elements at random. You could pick a random position in your collection, then swap the element in that position with the next element. This way, you can prevent swapping 1 with 3, or 2 with 4. You can do this repetitively, until the numbers are properly scrambled:

[1, 2, 3, 4] random number is 0, swap with element at position 1.

[2, 1, 3, 4] random number is 1, swap with element at position 2.

elements are 1 and 3, so don't swap.

[2, 1, 3, 4] random number is 2, swap with element at position 3.

[2, 1, 4, 3] etc.

If you'd like to generalize the constraint, you can simply change the condition. Instead of refusing to swap when the elements are either 1 and 3, or 2 and 4 (as in the example above), you could make sure the two elements at the positions to be swapped are not within 2 of each other, so something like if(b==a+2)continue; :

elements are 5 and 7, so don't swap.

if(7==5+2)continue; // ie don't swap.

If you use it as a string then you could use this answer's algorithm to swap all the numbers

When you enter all the numbers then just concatenate them together. There is no need to treat them as numbers or strings. All you want to do is reorder them.

When you get the result you could then check to see if your constraints match and then print out another list. Something like this perhaps

private boolean isConstraintSatisfied(String wholeString, String firstNum, String secondNum){
    return wholeString.indexOf(firstNum) <= wholeString.indexOf(secondNum);
}

Not the most elegant solution but I think it would work. For small input sets it shouldnt be too inefficient.

What you've defined here is known as a partial order . You wish to generate a random permutation which still satisfies the partial order, ie a random linear extension.

Luckily, the Java API specifies Collections.shuffle , which implements the Fisher-Yates algorithm to generate a random permutation. Unfortunately, the standard Java technique via Collections.sort is a comparison sort , and thus focused on total orders -- unlike the partial order we want. In fact, the Java API lacks a sorting algorithm we could use here.

One approach covered in "Generating Linear Extensions of Posets by Transpositions" involves that of swapping adjacent elements in the set in a fashion similar to Hassan's solution. This appears to be a functioning way for the localized problem at hand.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM