Split out random utils into their own class, added number checks
This commit is contained in:
parent
d3a95fa6ef
commit
36f6000b42
|
@ -19,80 +19,176 @@ package dorkbox.util;
|
||||||
public
|
public
|
||||||
class MathUtil {
|
class MathUtil {
|
||||||
|
|
||||||
private static final FastThreadLocal<MersenneTwisterFast> random = new FastThreadLocal<MersenneTwisterFast>() {
|
|
||||||
@Override
|
|
||||||
public
|
|
||||||
MersenneTwisterFast initialValue() {
|
|
||||||
return new MersenneTwisterFast();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates the thread local MersenneTwister (as it's not thread safe), if necessary
|
* Checks to see if the string is an integer
|
||||||
|
*
|
||||||
|
* @return true if it's an integer, false otherwise
|
||||||
*/
|
*/
|
||||||
public static
|
public static
|
||||||
MersenneTwisterFast random() {
|
boolean isInteger(final String string) {
|
||||||
return random.get();
|
return isNumber(string, 10);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a random integer
|
* Checks to see if the string is a long
|
||||||
|
*
|
||||||
|
* @return true if it's a long, false otherwise
|
||||||
*/
|
*/
|
||||||
public static
|
public static
|
||||||
int randomInt() {
|
boolean isLong(final String string) {
|
||||||
return random.get().nextInt();
|
return isNumber(string, 19);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a random number between 0 (inclusive) and the specified value (inclusive).
|
* Checks to see if the string is a number
|
||||||
|
*
|
||||||
|
* @return true if it's a number, false otherwise
|
||||||
*/
|
*/
|
||||||
public static
|
public static
|
||||||
int randomInt(int range) {
|
boolean isNumber(final String string, long sizeLimit) {
|
||||||
return random.get().nextInt(range + 1);
|
if (string == null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (sizeLimit <= 0) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
int length = string.length();
|
||||||
|
if (length == 0) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
int i = 0;
|
||||||
|
if (string.charAt(0) == '-') {
|
||||||
|
if (length == 1) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
i = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (length - i > sizeLimit) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (; i < length; i++) {
|
||||||
|
char c = string.charAt(i);
|
||||||
|
if (c < '0' || c > '9') {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a random number between start (inclusive) and end (inclusive).
|
* Gets the number of digits represented by the specified number
|
||||||
|
*
|
||||||
|
* @param number the number to check, negative numbers are also acceptable but the sign is not counted
|
||||||
|
*
|
||||||
|
* @return the number of digits of the number, from 1-19.
|
||||||
*/
|
*/
|
||||||
public static
|
public static
|
||||||
int randomInt(int start, int end) {
|
int numberOfDigits(long number) {
|
||||||
return start + random.get().nextInt(end - start + 1);
|
// have to make it always positive for the following checks to pass.
|
||||||
|
if (number < 0L) {
|
||||||
|
number = -number;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
// Guessing 4 digit numbers will be more probable.
|
||||||
* Returns a random boolean value.
|
// They are set in the first branch.
|
||||||
*/
|
if (number < 10000L) { // from 1 to 4
|
||||||
public static
|
if (number < 100L) { // 1 or 2
|
||||||
boolean randomBoolean() {
|
if (number < 10L) {
|
||||||
return random.get().nextBoolean();
|
return 1;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return 2;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else { // 3 or 4
|
||||||
|
if (number < 1000L) {
|
||||||
|
return 3;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return 4;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else { // from 5 to 20 (albeit longs can't have more than 18 or 19)
|
||||||
|
if (number < 1000000000000L) { // from 5 to 12
|
||||||
|
if (number < 100000000L) { // from 5 to 8
|
||||||
|
if (number < 1000000L) { // 5 or 6
|
||||||
|
if (number < 100000L) {
|
||||||
|
return 5;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return 6;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else { // 7 u 8
|
||||||
|
if (number < 10000000L) {
|
||||||
|
return 7;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return 8;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else { // from 9 to 12
|
||||||
|
if (number < 10000000000L) { // 9 or 10
|
||||||
|
if (number < 1000000000L) {
|
||||||
|
return 9;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return 10;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else { // 11 or 12
|
||||||
|
if (number < 100000000000L) {
|
||||||
|
return 11;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return 12;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else { // from 13 to ... (18 or 20)
|
||||||
|
if (number < 10000000000000000L) { // from 13 to 16
|
||||||
|
if (number < 100000000000000L) { // 13 or 14
|
||||||
|
if (number < 10000000000000L) {
|
||||||
|
return 13;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return 14;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else { // 15 or 16
|
||||||
|
if (number < 1000000000000000L) {
|
||||||
|
return 15;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return 16;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else { // from 17 to ... 20?
|
||||||
|
if (number < 1000000000000000000L) { // 17 or 18
|
||||||
|
if (number < 100000000000000000L) {
|
||||||
|
return 17;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return 18;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else { // 19? Can it be?
|
||||||
|
// 10000000000000000000L isn't a valid long.
|
||||||
|
return 19;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns random number between 0.0 (inclusive) and 1.0 (exclusive).
|
|
||||||
*/
|
|
||||||
public static
|
|
||||||
float randomFloat() {
|
|
||||||
return random.get().nextFloat();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns a random number between 0 (inclusive) and the specified value (exclusive).
|
|
||||||
*/
|
|
||||||
public static
|
|
||||||
float randomFloat(float range) {
|
|
||||||
return random.get().nextFloat() * range;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns a random number between start (inclusive) and end (exclusive).
|
|
||||||
*/
|
|
||||||
public static
|
|
||||||
float randomFloat(float start, float end) {
|
|
||||||
return start + random.get().nextFloat() * (end - start);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ---
|
|
||||||
|
|
||||||
|
|
||||||
public static
|
public static
|
||||||
boolean isEven(int value) {
|
boolean isEven(int value) {
|
||||||
|
|
93
src/dorkbox/util/RandomUtil.java
Normal file
93
src/dorkbox/util/RandomUtil.java
Normal file
|
@ -0,0 +1,93 @@
|
||||||
|
/*
|
||||||
|
* Copyright 2010 dorkbox, llc
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
package dorkbox.util;
|
||||||
|
|
||||||
|
|
||||||
|
public
|
||||||
|
class RandomUtil {
|
||||||
|
|
||||||
|
private static final FastThreadLocal<MersenneTwisterFast> random = new FastThreadLocal<MersenneTwisterFast>() {
|
||||||
|
@Override
|
||||||
|
public
|
||||||
|
MersenneTwisterFast initialValue() {
|
||||||
|
return new MersenneTwisterFast();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates the thread local MersenneTwister (as it's not thread safe), if necessary
|
||||||
|
*/
|
||||||
|
public static
|
||||||
|
MersenneTwisterFast get() {
|
||||||
|
return random.get();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a get integer
|
||||||
|
*/
|
||||||
|
public static
|
||||||
|
int int_() {
|
||||||
|
return get().nextInt();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a get number between 0 (inclusive) and the specified value (inclusive).
|
||||||
|
*/
|
||||||
|
public static
|
||||||
|
int int_(int range) {
|
||||||
|
return get().nextInt(range + 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a get number between start (inclusive) and end (inclusive).
|
||||||
|
*/
|
||||||
|
public static
|
||||||
|
int int_(int start, int end) {
|
||||||
|
return start + get().nextInt(end - start + 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a get boolean value.
|
||||||
|
*/
|
||||||
|
public static
|
||||||
|
boolean bool() {
|
||||||
|
return get().nextBoolean();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns get number between 0.0 (inclusive) and 1.0 (exclusive).
|
||||||
|
*/
|
||||||
|
public static
|
||||||
|
float float_() {
|
||||||
|
return get().nextFloat();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a get number between 0 (inclusive) and the specified value (exclusive).
|
||||||
|
*/
|
||||||
|
public static
|
||||||
|
float float_(float range) {
|
||||||
|
return get().nextFloat() * range;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a get number between start (inclusive) and end (exclusive).
|
||||||
|
*/
|
||||||
|
public static
|
||||||
|
float float_(float start, float end) {
|
||||||
|
return start + get().nextFloat() * (end - start);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user