|
22 | 22 | package org.cicirello.sequences;
|
23 | 23 |
|
24 | 24 | import java.util.random.RandomGenerator;
|
| 25 | +import org.cicirello.math.rand.RandomVariates; |
25 | 26 |
|
26 | 27 | /**
|
27 | 28 | * SequenceCompositeSampler generates random samples of array elements, without replacement.
|
@@ -153,6 +154,133 @@ public <T> T[] nextSample(T[] source, int k, T[] target) {
|
153 | 154 | return sample(source, k, target, r);
|
154 | 155 | }
|
155 | 156 |
|
| 157 | + /** |
| 158 | + * Generates a random sample, without replacement, from a given source array with a specified |
| 159 | + * probability of an element's inclusion in the sample. |
| 160 | + * |
| 161 | + * @param source The array from which to sample. |
| 162 | + * @param p The probability that element is included in the sample. The expected sample size is |
| 163 | + * source.length * p. |
| 164 | + * @param r The source of randomness. |
| 165 | + * @return An array containing the sample, whose sample size is simply the length of the array. |
| 166 | + */ |
| 167 | + public static int[] sample(int[] source, double p, RandomGenerator r) { |
| 168 | + return sample(source, RandomVariates.nextBinomial(source.length, p), null, r); |
| 169 | + } |
| 170 | + |
| 171 | + /** |
| 172 | + * Generates a random sample, without replacement, from a given source array with a specified |
| 173 | + * probability of an element's inclusion in the sample. |
| 174 | + * |
| 175 | + * @param source The array from which to sample. |
| 176 | + * @param p The probability that element is included in the sample. The expected sample size is |
| 177 | + * source.length * p. |
| 178 | + * @param r The source of randomness. |
| 179 | + * @return An array containing the sample, whose sample size is simply the length of the array. |
| 180 | + */ |
| 181 | + public static long[] sample(long[] source, double p, RandomGenerator r) { |
| 182 | + return sample(source, RandomVariates.nextBinomial(source.length, p), null, r); |
| 183 | + } |
| 184 | + |
| 185 | + /** |
| 186 | + * Generates a random sample, without replacement, from a given source array with a specified |
| 187 | + * probability of an element's inclusion in the sample. |
| 188 | + * |
| 189 | + * @param source The array from which to sample. |
| 190 | + * @param p The probability that element is included in the sample. The expected sample size is |
| 191 | + * source.length * p. |
| 192 | + * @param r The source of randomness. |
| 193 | + * @return An array containing the sample, whose sample size is simply the length of the array. |
| 194 | + */ |
| 195 | + public static short[] sample(short[] source, double p, RandomGenerator r) { |
| 196 | + return sample(source, RandomVariates.nextBinomial(source.length, p), null, r); |
| 197 | + } |
| 198 | + |
| 199 | + /** |
| 200 | + * Generates a random sample, without replacement, from a given source array with a specified |
| 201 | + * probability of an element's inclusion in the sample. |
| 202 | + * |
| 203 | + * @param source The array from which to sample. |
| 204 | + * @param p The probability that element is included in the sample. The expected sample size is |
| 205 | + * source.length * p. |
| 206 | + * @param r The source of randomness. |
| 207 | + * @return An array containing the sample, whose sample size is simply the length of the array. |
| 208 | + */ |
| 209 | + public static byte[] sample(byte[] source, double p, RandomGenerator r) { |
| 210 | + return sample(source, RandomVariates.nextBinomial(source.length, p), null, r); |
| 211 | + } |
| 212 | + |
| 213 | + /** |
| 214 | + * Generates a random sample, without replacement, from a given source array with a specified |
| 215 | + * probability of an element's inclusion in the sample. |
| 216 | + * |
| 217 | + * @param source The array from which to sample. |
| 218 | + * @param p The probability that element is included in the sample. The expected sample size is |
| 219 | + * source.length * p. |
| 220 | + * @param r The source of randomness. |
| 221 | + * @return An array containing the sample, whose sample size is simply the length of the array. |
| 222 | + */ |
| 223 | + public static double[] sample(double[] source, double p, RandomGenerator r) { |
| 224 | + return sample(source, RandomVariates.nextBinomial(source.length, p), null, r); |
| 225 | + } |
| 226 | + |
| 227 | + /** |
| 228 | + * Generates a random sample, without replacement, from a given source array with a specified |
| 229 | + * probability of an element's inclusion in the sample. |
| 230 | + * |
| 231 | + * @param source The array from which to sample. |
| 232 | + * @param p The probability that element is included in the sample. The expected sample size is |
| 233 | + * source.length * p. |
| 234 | + * @param r The source of randomness. |
| 235 | + * @return An array containing the sample, whose sample size is simply the length of the array. |
| 236 | + */ |
| 237 | + public static float[] sample(float[] source, double p, RandomGenerator r) { |
| 238 | + return sample(source, RandomVariates.nextBinomial(source.length, p), null, r); |
| 239 | + } |
| 240 | + |
| 241 | + /** |
| 242 | + * Generates a random sample, without replacement, from a given source array with a specified |
| 243 | + * probability of an element's inclusion in the sample. |
| 244 | + * |
| 245 | + * @param source The array from which to sample. |
| 246 | + * @param p The probability that element is included in the sample. The expected sample size is |
| 247 | + * source.length * p. |
| 248 | + * @param r The source of randomness. |
| 249 | + * @return An array containing the sample, whose sample size is simply the length of the array. |
| 250 | + */ |
| 251 | + public static char[] sample(char[] source, double p, RandomGenerator r) { |
| 252 | + return sample(source, RandomVariates.nextBinomial(source.length, p), null, r); |
| 253 | + } |
| 254 | + |
| 255 | + /** |
| 256 | + * Generates a random sample, without replacement, from a given source String with a specified |
| 257 | + * probability of an element's inclusion in the sample. |
| 258 | + * |
| 259 | + * @param source The String from which to sample. |
| 260 | + * @param p The probability that element is included in the sample. The expected sample size is |
| 261 | + * source.length() * p. |
| 262 | + * @param r The source of randomness. |
| 263 | + * @return An array containing the sample, whose sample size is simply the length of the array. |
| 264 | + */ |
| 265 | + public static char[] sample(String source, double p, RandomGenerator r) { |
| 266 | + return sample(source, RandomVariates.nextBinomial(source.length(), p), null, r); |
| 267 | + } |
| 268 | + |
| 269 | + /** |
| 270 | + * Generates a random sample, without replacement, from a given source array with a specified |
| 271 | + * probability of an element's inclusion in the sample. |
| 272 | + * |
| 273 | + * @param source The array from which to sample. |
| 274 | + * @param p The probability that element is included in the sample. The expected sample size is |
| 275 | + * source.length * p. |
| 276 | + * @param r The source of randomness. |
| 277 | + * @param <T> The type of array elements. |
| 278 | + * @return An array containing the sample, whose sample size is simply the length of the array. |
| 279 | + */ |
| 280 | + public static <T> T[] sample(T[] source, double p, RandomGenerator r) { |
| 281 | + return sample(source, RandomVariates.nextBinomial(source.length, p), null, r); |
| 282 | + } |
| 283 | + |
156 | 284 | /**
|
157 | 285 | * Generates a random sample of k elements, without replacement, from a given source array. All n
|
158 | 286 | * choose k combinations are equally likely, where n is the length of the source array.
|
|
0 commit comments