|
|
@@ -10,6 +10,7 @@ package net.ranides.assira.math;
|
|
|
import java.awt.Color;
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.List;
|
|
|
+import net.ranides.assira.collection.ArrayUtils;
|
|
|
import net.ranides.assira.reflection.ClassInspector;
|
|
|
|
|
|
/**
|
|
|
@@ -17,7 +18,7 @@ import net.ranides.assira.reflection.ClassInspector;
|
|
|
* @author ranides
|
|
|
*/
|
|
|
public final class Randomizer {
|
|
|
-
|
|
|
+
|
|
|
public static final String DIGITS = "1234567890";
|
|
|
public static final String ALPHA_LOWER = "qwertyuiopasdfghjklzxcvbnm";
|
|
|
public static final String ALPHA_UPPER = "QWERTYUIOPASDFGHJKLZXCVBNM";
|
|
|
@@ -42,7 +43,7 @@ public final class Randomizer {
|
|
|
public static long number(long max) {
|
|
|
return (long) Math.floor( Math.random() * max ) ;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
/**
|
|
|
* Losuje liczbę z zakresu [0;max)
|
|
|
* @param max
|
|
|
@@ -54,8 +55,8 @@ public final class Randomizer {
|
|
|
|
|
|
/**
|
|
|
* Losuje liczbę z zakresu [min;max]
|
|
|
- * @param min
|
|
|
- * @param max
|
|
|
+ * @param min
|
|
|
+ * @param max
|
|
|
* @return
|
|
|
*/
|
|
|
public static long number(long min, long max) {
|
|
|
@@ -73,14 +74,14 @@ public final class Randomizer {
|
|
|
|
|
|
/**
|
|
|
* Losuje liczbę z zakresu [min;max]
|
|
|
- * @param min
|
|
|
- * @param max
|
|
|
+ * @param min
|
|
|
+ * @param max
|
|
|
* @return
|
|
|
*/
|
|
|
public static int number(int min, int max) {
|
|
|
return min + (int)Math.round( Math.random() * (max-min) );
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
/**
|
|
|
* Losuje liczbę z zakresu [0;max)
|
|
|
* @param max
|
|
|
@@ -92,14 +93,14 @@ public final class Randomizer {
|
|
|
|
|
|
/**
|
|
|
* Losuje liczbę z zakresu [min;max]
|
|
|
- * @param min
|
|
|
- * @param max
|
|
|
+ * @param min
|
|
|
+ * @param max
|
|
|
* @return
|
|
|
*/
|
|
|
public static short number(short min, short max) {
|
|
|
return (short)(min + Math.round( Math.random() * (max-min) ));
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
/**
|
|
|
* Losuje liczbę z zakresu [min;max]
|
|
|
* @param min
|
|
|
@@ -119,19 +120,19 @@ public final class Randomizer {
|
|
|
public static float number(float min, float max) {
|
|
|
return min + (float)(Math.random() * (max-min));
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
public static float number(float max) {
|
|
|
return (float)(Math.random() * max);
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
public static double number(double min, double max) {
|
|
|
return min + (Math.random() * (max-min));
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
public static double number(double max) {
|
|
|
return Math.random() * max;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
/**
|
|
|
* Losuje liczbę z zakresu [0;max)
|
|
|
* @param max
|
|
|
@@ -143,18 +144,18 @@ public final class Randomizer {
|
|
|
|
|
|
/**
|
|
|
* Losuje liczbę z zakresu [min;max]
|
|
|
- * @param min
|
|
|
- * @param max
|
|
|
+ * @param min
|
|
|
+ * @param max
|
|
|
* @return
|
|
|
*/
|
|
|
public static char number(char min, char max) {
|
|
|
return (char)(min + Math.round( Math.random() * (max-min) ));
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
public static boolean bool() {
|
|
|
return Math.random() >= 0.5;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
/**
|
|
|
* Generuje losowy ciąg znaków, zawierający podane znaki, o długości {@code size}.
|
|
|
* @param size
|
|
|
@@ -181,7 +182,7 @@ public final class Randomizer {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * Losuje kolor o maksymalnym nasyceniu (Saturation), dowolnej barwie (Hue) i
|
|
|
+ * Losuje kolor o maksymalnym nasyceniu (Saturation), dowolnej barwie (Hue) i
|
|
|
* jasności z podanego zakresu.
|
|
|
* @param min
|
|
|
* @param max
|
|
|
@@ -214,7 +215,7 @@ public final class Randomizer {
|
|
|
public static char character() {
|
|
|
return character(ALPHANUM);
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
/**
|
|
|
* Zwraca losowy znak z podanego zbioru.
|
|
|
* @param chars
|
|
|
@@ -256,7 +257,7 @@ public final class Randomizer {
|
|
|
}
|
|
|
throw new UnsupportedOperationException("Unsupported type: " + type);
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
public static <T> List<T> list(Class<T> type, int size) {
|
|
|
List<T> list = new ArrayList<T>(size);
|
|
|
for(int i=0; i<size; i++) {
|
|
|
@@ -265,4 +266,14 @@ public final class Randomizer {
|
|
|
return list;
|
|
|
}
|
|
|
|
|
|
+ @SuppressWarnings("unchecked")
|
|
|
+ public static <Auto> Auto array(Class<?> type, int size) {
|
|
|
+ Object array = ArrayUtils.make(type, size);
|
|
|
+ List<Object> list = ArrayUtils.wrap(array);
|
|
|
+ for(int i=0; i<size; i++) {
|
|
|
+ list.add(object(type));
|
|
|
+ }
|
|
|
+ return (Auto)array;
|
|
|
+ }
|
|
|
+
|
|
|
}
|