-
Notifications
You must be signed in to change notification settings - Fork 0
/
RandomShuffle.java
82 lines (69 loc) · 2.93 KB
/
RandomShuffle.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
public class RandomShuffle extends ClassicalCipher
{ // Not a cipher, just a helpful function
/*
Copyright (C) 2019 S Combes
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
// ---------------------------------------------------------------------------
RandomShuffle() { this (new Codespace(Codespace.StockAlphabet.CAPITALS)); }
RandomShuffle(Codespace cs)
{
super(cs);
if (!cs.PTspace.equals(cs.CTspace)) throw new IllegalArgumentException(
"Shuffle must have identical character spaces for PT and CT");
}
// ---------------------------------------------------------------------------
@Override
public String encode(String text) { return shuffle(cs.flattenToPT(text)); }
// ---------------------------------------------------------------------------
@Override
public String decode(String text) { return encode(text); } // Codespaces are same
// ---------------------------------------------------------------------------
public static String shuffle(String text) {
// The static equivalent
StringBuilder sb=new StringBuilder(text.length());
int [] items=randomPermutation(text.length());
for (int i=0;i<items.length;i++)
sb.append(text.charAt(items[i]));
return sb.toString();
}
// ---------------------------------------------------------------------------
public static int [] randomPermutation(int n) // Public - has other uses.
{
// Fischer-Yates Shuffle ('inside-out' algorithm)
// http://en.wikipedia.org/w/index.php?title=Fisher%E2%80%93Yates_shuffle&oldid=597499563
int [] items=new int[n];
items[0]=0;
for (int i=1;i<n;i++) {
int j=rand.nextInt(i+1); // i correct, not n. +1 because nextInt is exclusive for end index
if (j!=i) items[i]=items[j];
items[j]=i;
}
return items;
}
// ---------------------------------------------------------------------------
public static void main(String [] args) {
RandomShuffle rs=new RandomShuffle();
for (int i=0;i<10;i++)
System.out.println(rs.encode("ABCDEF"));
String test="ABCDEFGHIJKLMNO";
int [] freq=new int[test.length()];
for (int i=0;i<10000;i++) {
String out=rs.encode(test);
for (int j=0;j<test.length();j++)
if (test.indexOf(out.charAt(j))==j)
freq[j]++;
}
for (int i=0;i<freq.length;i++)
System.out.println(i+" "+freq[i]);
System.out.println("Distribution should be uniform (statistically)");
}
}