Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
35
Task/Knuths-algorithm-S/Java/knuths-algorithm-s-1.java
Normal file
35
Task/Knuths-algorithm-S/Java/knuths-algorithm-s-1.java
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
import java.util.*;
|
||||
|
||||
class SOfN<T> {
|
||||
private static final Random rand = new Random();
|
||||
|
||||
private List<T> sample;
|
||||
private int i = 0;
|
||||
private int n;
|
||||
|
||||
public SOfN(int _n) {
|
||||
n = _n;
|
||||
sample = new ArrayList<T>(n);
|
||||
}
|
||||
|
||||
public List<T> process(T item) {
|
||||
if (++i <= n) {
|
||||
sample.add(item);
|
||||
} else if (rand.nextInt(i) < n) {
|
||||
sample.set(rand.nextInt(n), item);
|
||||
}
|
||||
return sample;
|
||||
}
|
||||
}
|
||||
|
||||
public class AlgorithmS {
|
||||
public static void main(String[] args) {
|
||||
int[] bin = new int[10];
|
||||
for (int trial = 0; trial < 100000; trial++) {
|
||||
SOfN<Integer> s_of_n = new SOfN<Integer>(3);
|
||||
for (int i = 0; i < 9; i++) s_of_n.process(i);
|
||||
for (int s : s_of_n.process(9)) bin[s]++;
|
||||
}
|
||||
System.out.println(Arrays.toString(bin));
|
||||
}
|
||||
}
|
||||
33
Task/Knuths-algorithm-S/Java/knuths-algorithm-s-2.java
Normal file
33
Task/Knuths-algorithm-S/Java/knuths-algorithm-s-2.java
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
import java.util.*;
|
||||
|
||||
interface Function<S, T> {
|
||||
public T call(S x);
|
||||
}
|
||||
|
||||
public class AlgorithmS {
|
||||
private static final Random rand = new Random();
|
||||
public static <T> Function<T, List<T>> s_of_n_creator(final int n) {
|
||||
return new Function<T, List<T>>() {
|
||||
private List<T> sample = new ArrayList<T>(n);
|
||||
private int i = 0;
|
||||
public List<T> call(T item) {
|
||||
if (++i <= n) {
|
||||
sample.add(item);
|
||||
} else if (rand.nextInt(i) < n) {
|
||||
sample.set(rand.nextInt(n), item);
|
||||
}
|
||||
return sample;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
int[] bin = new int[10];
|
||||
for (int trial = 0; trial < 100000; trial++) {
|
||||
Function<Integer, List<Integer>> s_of_n = s_of_n_creator(3);
|
||||
for (int i = 0; i < 9; i++) s_of_n.call(i);
|
||||
for (int s : s_of_n.call(9)) bin[s]++;
|
||||
}
|
||||
System.out.println(Arrays.toString(bin));
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue