KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > stockonline > database > oracle > RandGenerator


1 /*
2  * StockOnline: EJB 1.1 Benchmark.
3  *
4  * Copyright © Commonwealth Scientific and Industrial Research Organisation (CSIRO - www.csiro.au), Australia 2001, 2002, 2003.
5  *
6  * Contact: Paul.Brebner@csiro.au
7  *
8  * This library is free software; you can redistribute it and/or modify it
9  * under the terms of the GNU Lesser General Public License as published by
10  * the Free Software Foundation; either version 2.1 of the License, or any
11  * later version.
12  *
13  * This library is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
16  * for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License
19  * along with this library; if not, write to the Free Software Foundation,
20  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
21  *
22  * Originally developed for the CSIRO Middleware Technology Evaluation (MTE) Project, by
23  * the Software Architectures and Component Technologies Group, CSIRO Mathematical and Information Sciences
24  * Canberra and Sydney, Australia
25  *
26  * www.cmis.csiro.au/sact/
27  * www.cmis.csiro.au/adsat/mte.htm
28  *
29  * Initial developer(s): Shiping Chen, Paul Brebner, Lei Hu, Shuping Ran, Ian Gorton, Anna Liu.
30  * Contributor(s): ______________________.
31  */

32
33
34 package stockonline.database.oracle;
35
36 public class RandGenerator
37 {
38     private static java.util.Random JavaDoc _r = new java.util.Random JavaDoc();
39
40     public static String JavaDoc _randomStrFixLen(java.util.Random JavaDoc r, int length)
41     {
42         String JavaDoc str = "";
43         for (int i=0; i<length; i++)
44     {
45         str += (char)((int)'A' + Math.abs(r.nextInt())%25);
46     }
47         return str;
48     }
49
50     public static String JavaDoc _randomStrChaLen(java.util.Random JavaDoc r, int seed)
51     {
52         String JavaDoc str = "";
53
54     r.setSeed(seed);
55     int length = Math.abs(r.nextInt())%7 + 3;
56
57         for (int i=0; i<length; i++)
58     {
59         str += (char)((int)'A'+Math.abs(r.nextInt())%25);
60     }
61         return str;
62     }
63
64     public static String JavaDoc randomStrFixLen(int length)
65     {
66         return _randomStrFixLen(_r, length);
67     }
68
69     public static String JavaDoc randomStrChaLen(int seed)
70     {
71         return _randomStrChaLen(_r, seed);
72     }
73
74     public static int randomInt(java.util.Random JavaDoc r, int a, int b)
75     {
76         return (Math.abs(r.nextInt()) % (b-a-1) + a);
77     }
78
79     /**
80      * @return an integer between a (inclusive) and b (exclusive)
81      */

82     public static int randomInt(int a, int b)
83     {
84         return randomInt(_r, a, b);
85     }
86
87     public static float randomFloat(java.util.Random JavaDoc r, float a, float b)
88     {
89         return Math.abs(r.nextFloat())*(b-a-1.0F) + a;
90     }
91
92     /**
93      * @return a float between a (inclusive) and b (exclusive)
94      */

95     public static float randomFloat(float a, float b)
96     {
97         return randomFloat(_r, a, b);
98     }
99
100     public static void main(String JavaDoc args[])
101     {
102             for(int i=0; i<10; i++)
103                 System.out.println(RandGenerator.randomStrFixLen(10));
104
105             for(int i=0; i<10; i++)
106                 System.out.println(RandGenerator.randomStrChaLen(i));
107
108             for(int i=0; i<10; i++)
109                 System.out.println(RandGenerator.randomInt(10, 20));
110
111             for(int i=0; i<10; i++)
112                 System.out.println(RandGenerator.randomFloat(1.6F, 2.3F));
113     }
114 }
Popular Tags