From 89cdf3efb49335e7c07a68a5a64657eeec2288a6 Mon Sep 17 00:00:00 2001 From: Alexander Pickering Date: Mon, 6 Feb 2017 11:41:36 -0500 Subject: Inital commit --- .../project1_frequencyBag/RandomDistribution.java | 77 ++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 projects/project1_frequencyBag/RandomDistribution.java (limited to 'projects/project1_frequencyBag/RandomDistribution.java') 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; + } +} -- cgit v1.2.3-70-g09d2