forked from functionaljava/functionaljava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRand.java
More file actions
156 lines (143 loc) · 4.95 KB
/
Rand.java
File metadata and controls
156 lines (143 loc) · 4.95 KB
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package fj.test;
import fj.F;
import fj.data.Option;
import static fj.data.Option.some;
import static java.lang.Math.max;
import static java.lang.Math.min;
import java.util.Random;
/**
* A random number generator.
*
* @version %build.number%
*/
public final class Rand {
private final F<Option<Long>, F<Integer, F<Integer, Integer>>> f;
private final F<Option<Long>, F<Double, F<Double, Double>>> g;
private Rand(final F<Option<Long>, F<Integer, F<Integer, Integer>>> f, final F<Option<Long>, F<Double, F<Double, Double>>> g) {
this.f = f;
this.g = g;
}
/**
* Randomly chooses a value between the given range (inclusive).
*
* @param seed The seed to use for random generation.
* @param from The minimum value to choose.
* @param to The maximum value to choose.
* @return A random value in the given range.
*/
public int choose(final long seed, final int from, final int to) {
return f.f(some(seed)).f(from).f(to);
}
/**
* Randomly chooses a value between the given range (inclusive).
*
* @param from The minimum value to choose.
* @param to The maximum value to choose.
* @return A random value in the given range.
*/
public int choose(final int from, final int to) {
return f.f(Option.<Long>none()).f(from).f(to);
}
/**
* Randomly chooses a value between the given range (inclusive).
*
* @param seed The seed to use for random generation.
* @param from The minimum value to choose.
* @param to The maximum value to choose.
* @return A random value in the given range.
*/
public double choose(final long seed, final double from, final double to) {
return g.f(some(seed)).f(from).f(to);
}
/**
* Randomly chooses a value between the given range (inclusive).
*
* @param from The minimum value to choose.
* @param to The maximum value to choose.
* @return A random value in the given range.
*/
public double choose(final double from, final double to) {
return g.f(Option.<Long>none()).f(from).f(to);
}
/**
* Gives this random generator a new seed.
*
* @param seed The seed of the new random generator.
* @return A random generator with the given seed.
*/
public Rand reseed(final long seed) {
return new Rand(new F<Option<Long>, F<Integer, F<Integer, Integer>>>() {
public F<Integer, F<Integer, Integer>> f(final Option<Long> old) {
return new F<Integer, F<Integer, Integer>>() {
public F<Integer, Integer> f(final Integer from) {
return new F<Integer, Integer>() {
public Integer f(final Integer to) {
return f.f(some(seed)).f(from).f(to);
}
};
}
};
}
}, new F<Option<Long>, F<Double, F<Double, Double>>>() {
public F<Double, F<Double, Double>> f(final Option<Long> old) {
return new F<Double, F<Double, Double>>() {
public F<Double, Double> f(final Double from) {
return new F<Double, Double>() {
public Double f(final Double to) {
return g.f(some(seed)).f(from).f(to);
}
};
}
};
}
});
}
/**
* Constructs a random generator from the given functions that supply a range to produce a
* result.
*
* @param f The integer random generator.
* @param g The floating-point random generator.
* @return A random generator from the given functions that supply a range to produce a result.
*/
public static Rand rand(final F<Option<Long>, F<Integer, F<Integer, Integer>>> f, final F<Option<Long>, F<Double, F<Double, Double>>> g) {
return new Rand(f, g);
}
private static final F<Long, Random> fr = new F<Long, Random>() {
public Random f(final Long x) {
return new Random(x);
}
};
/**
* A standard random generator that uses {@link Random}.
*/
public static final Rand standard = new Rand(new F<Option<Long>, F<Integer, F<Integer, Integer>>>() {
public F<Integer, F<Integer, Integer>> f(final Option<Long> seed) {
return new F<Integer, F<Integer, Integer>>() {
public F<Integer, Integer> f(final Integer from) {
return new F<Integer, Integer>() {
public Integer f(final Integer to) {
final int f = min(from, to);
final int t = max(from, to);
return f + seed.map(fr).orSome(new Random()).nextInt(t - f + 1);
}
};
}
};
}
}, new F<Option<Long>, F<Double, F<Double, Double>>>() {
public F<Double, F<Double, Double>> f(final Option<Long> seed) {
return new F<Double, F<Double, Double>>() {
public F<Double, Double> f(final Double from) {
return new F<Double, Double>() {
public Double f(final Double to) {
final double f = min(from, to);
final double t = max(from, to);
return seed.map(fr).orSome(new Random()).nextDouble() * (t - f) + f;
}
};
}
};
}
});
}