Last change
on this file since 52 was 52, checked in by ruud, 14 months ago |
Fixed small issues in domaineditor.
|
File size:
718 bytes
|
Line | |
---|
1 | package geniusweb.ip.general;
|
---|
2 |
|
---|
3 | import java.util.Random;
|
---|
4 |
|
---|
5 | public class RandomPermutation {
|
---|
6 |
|
---|
7 | private int n;
|
---|
8 | private int[] availableNumbersToChooseFrom;
|
---|
9 |
|
---|
10 | public RandomPermutation(int n) {
|
---|
11 | this.n = n;
|
---|
12 | this.availableNumbersToChooseFrom = new int[n];
|
---|
13 | }
|
---|
14 |
|
---|
15 | // random permutation of numbers (1, ..., n)
|
---|
16 | public int[] get() {
|
---|
17 | Random randomGenerator = new Random();
|
---|
18 | for (int i = n - 1; i >= 0; i--) {
|
---|
19 | availableNumbersToChooseFrom[i] = i;
|
---|
20 | }
|
---|
21 | int[] permutation = new int[n];
|
---|
22 | for (int i = n - 1; i >= 0; i--) {
|
---|
23 | int j = randomGenerator.nextInt(i + 1);
|
---|
24 | permutation[i] = availableNumbersToChooseFrom[j] + 1;
|
---|
25 | availableNumbersToChooseFrom[j] = availableNumbersToChooseFrom[i];
|
---|
26 | }
|
---|
27 | return permutation;
|
---|
28 | }
|
---|
29 | }
|
---|
Note:
See
TracBrowser
for help on using the repository browser.