Source Code Cross Referenced for RandomGen.java in  » Database-DBMS » h2database » org » h2 » test » synth » sql » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Database DBMS » h2database » org.h2.test.synth.sql 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Copyright 2004-2008 H2 Group. Licensed under the H2 License, Version 1.0
003:         * (license2)
004:         * Initial Developer: H2 Group
005:         */
006:        package org.h2.test.synth.sql;
007:
008:        import java.sql.Date;
009:        import java.sql.Time;
010:        import java.sql.Timestamp;
011:        import java.util.Random;
012:
013:        /**
014:         * A random data generator class.
015:         */
016:        public class RandomGen {
017:            private Random random = new Random();
018:
019:            // private TestSynth config;
020:
021:            public RandomGen(TestSynth config) {
022:                // this.config = config;
023:                random.setSeed(12);
024:            }
025:
026:            public int getInt(int max) {
027:                return max == 0 ? 0 : random.nextInt(max);
028:            }
029:
030:            public double nextGaussian() {
031:                return random.nextGaussian();
032:            }
033:
034:            public int getLog(int max) {
035:                if (max == 0) {
036:                    return 0;
037:                }
038:                while (true) {
039:                    int d = Math.abs((int) (random.nextGaussian() / 2. * max));
040:                    if (d < max) {
041:                        return d;
042:                    }
043:                }
044:            }
045:
046:            public void getBytes(byte[] data) {
047:                random.nextBytes(data);
048:            }
049:
050:            public boolean getBoolean(int percent) {
051:                return random.nextInt(100) <= percent;
052:            }
053:
054:            public String randomString(int len) {
055:                StringBuffer buff = new StringBuffer();
056:                for (int i = 0; i < len; i++) {
057:                    String from = (i % 2 == 0) ? "bdfghklmnpqrst" : "aeiou";
058:                    buff.append(from.charAt(getInt(from.length())));
059:                }
060:                return buff.toString();
061:            }
062:
063:            public int getRandomInt() {
064:                switch (random.nextInt(10)) {
065:                case 0:
066:                    return Integer.MAX_VALUE;
067:                case 1:
068:                    return Integer.MIN_VALUE;
069:                case 2:
070:                    return random.nextInt();
071:                case 3:
072:                case 4:
073:                    return 0;
074:                case 5:
075:                    return (int) (random.nextGaussian() * 2000) - 200;
076:                default:
077:                    return (int) (random.nextGaussian() * 20) - 5;
078:                }
079:            }
080:
081:            public long getRandomLong() {
082:                switch (random.nextInt(10)) {
083:                case 0:
084:                    return Long.MAX_VALUE;
085:                case 1:
086:                    return Long.MIN_VALUE;
087:                case 2:
088:                    return random.nextLong();
089:                case 3:
090:                case 4:
091:                    return 0;
092:                case 5:
093:                    return (int) (random.nextGaussian() * 20000) - 2000;
094:                default:
095:                    return (int) (random.nextGaussian() * 200) - 50;
096:                }
097:            }
098:
099:            public double getRandomDouble() {
100:                switch (random.nextInt(10)) {
101:                case 0:
102:                    return Double.MIN_VALUE;
103:                case 1:
104:                    return Double.MAX_VALUE;
105:                case 2:
106:                    return Float.MIN_VALUE;
107:                case 3:
108:                    return Float.MAX_VALUE;
109:                case 4:
110:                    return random.nextDouble();
111:                case 5:
112:                case 6:
113:                    return 0;
114:                case 7:
115:                    return random.nextGaussian() * 20000. - 2000.;
116:                default:
117:                    return random.nextGaussian() * 200. - 50.;
118:                }
119:            }
120:
121:            public boolean nextBoolean() {
122:                return random.nextBoolean();
123:            }
124:
125:            public int[] getIntArray() {
126:                switch (random.nextInt(10)) {
127:                case 0:
128:                    return null;
129:                default:
130:                    int len = getInt(100);
131:                    int[] list = new int[len];
132:                    for (int i = 0; i < len; i++) {
133:                        list[i] = getRandomInt();
134:                    }
135:                    return list;
136:                }
137:            }
138:
139:            public Object getByteArray() {
140:                switch (random.nextInt(10)) {
141:                case 0:
142:                    return null;
143:                default:
144:                    int len = getInt(100);
145:                    byte[] list = new byte[len];
146:                    random.nextBytes(list);
147:                    return list;
148:                }
149:            }
150:
151:            public Time randomTime() {
152:                if (random.nextInt(10) == 0) {
153:                    return null;
154:                }
155:                StringBuffer buff = new StringBuffer();
156:                buff.append(getInt(24));
157:                buff.append(':');
158:                buff.append(getInt(24));
159:                buff.append(':');
160:                buff.append(getInt(24));
161:                return Time.valueOf(buff.toString());
162:
163:            }
164:
165:            public Timestamp randomTimestamp() {
166:                if (random.nextInt(10) == 0) {
167:                    return null;
168:                }
169:                StringBuffer buff = new StringBuffer();
170:                buff.append(getInt(10) + 2000);
171:                buff.append('-');
172:                int month = getInt(12) + 1;
173:                if (month < 10) {
174:                    buff.append('0');
175:                }
176:                buff.append(month);
177:                buff.append('-');
178:                int day = getInt(28) + 1;
179:                if (day < 10) {
180:                    buff.append('0');
181:                }
182:                buff.append(day);
183:                buff.append(' ');
184:                int hour = getInt(24);
185:                if (hour < 10) {
186:                    buff.append('0');
187:                }
188:                buff.append(hour);
189:                buff.append(':');
190:                int minute = getInt(60);
191:                if (minute < 10) {
192:                    buff.append('0');
193:                }
194:                buff.append(minute);
195:                buff.append(':');
196:                int second = getInt(60);
197:                if (second < 10) {
198:                    buff.append('0');
199:                }
200:                buff.append(second);
201:                // TODO test timestamp nanos
202:                return Timestamp.valueOf(buff.toString());
203:            }
204:
205:            public Date randomDate() {
206:                if (random.nextInt(10) == 0) {
207:                    return null;
208:                }
209:                StringBuffer buff = new StringBuffer();
210:                buff.append(getInt(10) + 2000);
211:                buff.append('-');
212:                buff.append(getInt(11) + 1);
213:                buff.append('-');
214:                buff.append(getInt(29) + 1);
215:                return Date.valueOf(buff.toString());
216:            }
217:
218:            public String modify(String sql) {
219:                int len = getLog(10);
220:                for (int i = 0; i < len; i++) {
221:                    int pos = getInt(sql.length());
222:                    if (getBoolean(50)) {
223:                        String badChars = "abcABCDEF\u00ef\u00f6\u00fcC1230=<>+\"\\*%&/()=?$_-.:,;{}[]";
224:                        // auml, ouml, uuml
225:                        char bad = badChars.charAt(getInt(badChars.length()));
226:                        sql = sql.substring(0, pos) + bad + sql.substring(pos);
227:                    } else {
228:                        if (pos >= sql.length()) {
229:                            sql = sql.substring(0, pos);
230:                        } else {
231:                            sql = sql.substring(0, pos)
232:                                    + sql.substring(pos + 1);
233:                        }
234:                    }
235:                }
236:                return sql;
237:            }
238:
239:            public void setSeed(int seed) {
240:                random.setSeed(seed);
241:            }
242:
243:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.