Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/demo/share/jfc/SwingSet2/Permuter.java
41149 views
1
/*
2
*
3
* Copyright (c) 2007, Oracle and/or its affiliates. All rights reserved.
4
*
5
* Redistribution and use in source and binary forms, with or without
6
* modification, are permitted provided that the following conditions
7
* are met:
8
*
9
* - Redistributions of source code must retain the above copyright
10
* notice, this list of conditions and the following disclaimer.
11
*
12
* - Redistributions in binary form must reproduce the above copyright
13
* notice, this list of conditions and the following disclaimer in the
14
* documentation and/or other materials provided with the distribution.
15
*
16
* - Neither the name of Oracle nor the names of its
17
* contributors may be used to endorse or promote products derived
18
* from this software without specific prior written permission.
19
*
20
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
21
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
*/
32
33
34
35
import java.util.Random;
36
37
/**
38
* An object that implements a cheesy pseudorandom permutation of the integers
39
* from zero to some user-specified value. (The permutation is a linear
40
* function.)
41
*
42
* @author Josh Bloch
43
*/
44
class Permuter {
45
/**
46
* The size of the permutation.
47
*/
48
private int modulus;
49
50
/**
51
* Nonnegative integer less than n that is relatively prime to m.
52
*/
53
private int multiplier;
54
55
/**
56
* Pseudorandom nonnegative integer less than n.
57
*/
58
private int addend = 22;
59
60
public Permuter(int n) {
61
if (n<0) {
62
throw new IllegalArgumentException();
63
}
64
modulus = n;
65
if (n==1) {
66
return;
67
}
68
69
// Initialize the multiplier and offset
70
multiplier = (int) Math.sqrt(n);
71
while (gcd(multiplier, n) != 1) {
72
if (++multiplier == n) {
73
multiplier = 1;
74
}
75
}
76
}
77
78
/**
79
* Returns the integer to which this permuter maps the specified integer.
80
* The specified integer must be between 0 and n-1, and the returned
81
* integer will be as well.
82
*/
83
public int map(int i) {
84
return (multiplier * i + addend) % modulus;
85
}
86
87
/**
88
* Calculate GCD of a and b, which are assumed to be non-negative.
89
*/
90
private static int gcd(int a, int b) {
91
while(b != 0) {
92
int tmp = a % b;
93
a = b;
94
b = tmp;
95
}
96
return a;
97
}
98
99
/**
100
* Simple test. Takes modulus on command line and prints out permutation.
101
*/
102
public static void main(String[] args) {
103
int modulus = Integer.parseInt(args[0]);
104
Permuter p = new Permuter(modulus);
105
for (int i=0; i<modulus; i++) {
106
System.out.print(p.map(i)+" ");
107
}
108
System.out.println();
109
}
110
}
111
112