diff options
Diffstat (limited to 'projects/project1_frequencyBag/RandomDistribution.java')
| -rw-r--r-- | projects/project1_frequencyBag/RandomDistribution.java | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/projects/project1_frequencyBag/RandomDistribution.java b/projects/project1_frequencyBag/RandomDistribution.java new file mode 100644 index 0000000..4e8bc0f --- /dev/null +++ b/projects/project1_frequencyBag/RandomDistribution.java @@ -0,0 +1,77 @@ +import java.util.Random; + + +public class RandomDistribution +{ + public static int normalDistributionInt(int min, int max) + { + int result; + do + { + result = (int) Math.round(boxMuller() * 100); + } + while(result < min || result > max); + + return result; + } + + private static double boxMuller() + { + Random rand = new Random(); + double u = rand.nextDouble(); + double v = rand.nextDouble(); + return Math.sqrt(-2 * (Math.log(u)/Math.log(Math.E))) * Math.cos(2 * Math.PI * v); + } + + public static int laplaceDistributionInt(int min, int max) + { + int result; + do + { + result = (int) Math.round(laplaceDistribution() * 100); + } + while(result < min || result > max); + + return result; + } + + private static double laplaceDistribution() + { + Random rand = new Random(); + double u = rand.nextDouble() - 0.5; + + return 0 - (Math.signum(u) * Math.log(1 - (2 * Math.abs(u)))); + } + + public static int uniformDistributionInt(int min, int max) + { + Random rand = new Random(); + + return rand.nextInt((max - min) + 1) + min; + } + + public static int clockDistribution(int min, int max, int hWidth, int lWidth) + { + int result; + + do + { + result = uniformDistributionInt(min, max); + int modResult = result % (hWidth + lWidth); + if(modResult < hWidth) + { + break; + } + else + { + if(uniformDistributionInt(0,1) == 1) + { + break; + } + } + } + while(true); + + return result; + } +} |
