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.bench;
007:
008: import java.math.BigDecimal;
009: import java.math.BigInteger;
010: import java.util.Random;
011:
012: /**
013: * The random data generator used for BenchC.
014: */
015: public class BenchCRandom {
016:
017: private Random random = new Random(10);
018:
019: int getNonUniform(int a, int min, int max) {
020: int c = 0;
021: return (((getInt(0, a) | getInt(min, max)) + c) % (max - min + 1))
022: + min;
023: }
024:
025: int getInt(int min, int max) {
026: return max <= min ? min : (random.nextInt(max - min) + min);
027: }
028:
029: boolean[] getBoolean(int length, int trueCount) {
030: boolean[] data = new boolean[length];
031: for (int i = 0, pos; i < trueCount; i++) {
032: do {
033: pos = getInt(0, length);
034: } while (data[pos]);
035: data[pos] = true;
036: }
037: return data;
038: }
039:
040: String replace(String text, String replacement) {
041: int pos = getInt(0, text.length() - replacement.length());
042: StringBuffer buffer = new StringBuffer(text);
043: buffer.replace(pos, pos + 7, replacement);
044: return buffer.toString();
045: }
046:
047: String getNumberString(int min, int max) {
048: int len = getInt(min, max);
049: char[] buff = new char[len];
050: for (int i = 0; i < len; i++) {
051: buff[i] = (char) getInt('0', '9');
052: }
053: return new String(buff);
054: }
055:
056: String[] getAddress() {
057: String str1 = getString(10, 20);
058: String str2 = getString(10, 20);
059: String city = getString(10, 20);
060: String state = getString(2);
061: String zip = getNumberString(9, 9);
062: return new String[] { str1, str2, city, state, zip };
063: }
064:
065: String getString(int min, int max) {
066: return getString(getInt(min, max));
067: }
068:
069: String getString(int len) {
070: char[] buff = new char[len];
071: for (int i = 0; i < len; i++) {
072: buff[i] = (char) getInt('A', 'Z');
073: }
074: return new String(buff);
075: }
076:
077: int[] getPermutation(int length) {
078: int[] data = new int[length];
079: for (int i = 0; i < length; i++) {
080: data[i] = i;
081: }
082: for (int i = 0; i < length; i++) {
083: int j = getInt(0, length);
084: int temp = data[i];
085: data[i] = data[j];
086: data[j] = temp;
087: }
088: return data;
089: }
090:
091: BigDecimal getBigDecimal(int value, int scale) {
092: return new BigDecimal(new BigInteger(String.valueOf(value)),
093: scale);
094: }
095:
096: String getLastname(int i) {
097: String[] n = { "BAR", "OUGHT", "ABLE", "PRI", "PRES", "ESE",
098: "ANTI", "CALLY", "ATION", "EING" };
099: StringBuffer buff = new StringBuffer();
100: buff.append(n[i / 100]);
101: buff.append(n[(i / 10) % 10]);
102: buff.append(n[i % 10]);
103: return buff.toString();
104: }
105:
106: }
|