/Users/lyon/j4p/src/math/MathUtils.java
|
1 /*
2 * Created by IntelliJ IDEA.
3 * User: lyon
4 * Date: Feb 15, 2003
5 * Time: 1:22:04 PM
6 */
7 package math;
8
9 import futils.WriterUtil;
10
11 import java.awt.*;
12
13 public class MathUtils {
14 public static int getRandom() {
15 return (int) ((Math.random() * 100) + 1);
16 }
17
18 public static void main(String args[]) {
19 writeOutRandomNumbersToAFile();
20 }
21
22 public static void writeOutRandomNumbersToAFile() {
23 int ia[] = new int[100];
24 for (int i = 0; i < ia.length; i++)
25 ia[i] = rand(1, 100);
26 WriterUtil.writeString(
27 WriterUtil.getCSVString(ia));
28 }
29
30 public static void randTest() {
31 for (int i = 0; i < 100; i++) {
32 System.out.print(rand(1, 10) + " ");
33 if (i % 20 == 0) System.out.println();
34 }
35 }
36
37 /**
38 * Compute a random integer
39 * @param min minimum int
40 * @param max maximum int
41 * @return random int between <code>min</code> and
42 * <code>max</code> inclusive.
43 */
44 public static int rand(int min, int max) {
45 return (int) (Math.round(
46 Math.random() * (max - min) + min));
47 }
48 public static Color getRandomColor() {
49 return new Color(rand(0,255),rand(0,255),rand(0,255));
50 }
51
52 public static boolean odd(int i) {
53 if ((i & 1) == 1) return true;
54 return false;
55 }
56
57 /**
58 *
59 * @param d input argument to the log2 function
60 * @return base 2 log.
61 */
62 public static int getLogBase2(double d) {
63 return (int) (Math.log(d) / Math.log(2));
64 }
65
66 public static final double getGauss(
67 double x,
68 double xc, double sigma) {
69 double oneOnSigmaSquaredOn2 = 1 / (sigma * sigma) / 2;
70 return
71 Math.exp(-((x - xc) * (x - xc)) *
72 oneOnSigmaSquaredOn2) / Math.PI * oneOnSigmaSquaredOn2;
73 }
74
75 public static final double[] getGauss(int n) {
76 double g[] = new double[n];
77 for (int x = 0; x < n; x++)
78 g[x] = getGauss(x, n / 2, 2);
79 return g;
80 }
81
82 public static final void testGauss() {
83 print(getGauss(10));
84 }
85
86 public static final void print(double d[]) {
87 for (int i = 0; i < d.length; i++)
88 System.out.println(d[i]);
89 }
90 }
91