Package com.github.toolarium.common.util
Class RandomGenerator
- java.lang.Object
-
- com.github.toolarium.common.util.RandomGenerator
-
public final class RandomGenerator extends java.lang.ObjectThis class implements some helper methods for random numbers. It works internal with the SecureRandom class.
-
-
Field Summary
Fields Modifier and Type Field Description static longONE_DAYThe miliseconds of a daystatic char[]validLetterCharactersDefines valid letter charactersstatic char[]validLowerCaseLetterCharactersDefines valid lower case letter charactersstatic char[]validNumberCharactersDefines valid number charactersstatic char[]validNumberLetterCharactersDefines valid number and letter charactersstatic char[]validUpperCaseLetterCharactersDefines valid upper case letter characters
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description floatchisQuare(int number, int size)Method to test the randomness of the random algorithm.static char[]combineValidCharacters(char[] input, char[] input2)Helper methods to combines two character arraysjava.lang.StringcreateGUID()Generates a GUID: GUIDs are guaranteed to be globally unique by using ethernet MACs, IP addresses, time elements, and sequential numbers.java.lang.StringcreateUUID()Creates a UUIDjava.lang.BooleangetBooleanRandom()Returns a random number as a Booleanjava.lang.DoublegetDoubleRandom()Returns a random number as a Doublejava.lang.FloatgetFloatRandom()Returns a random number as a Floatstatic RandomGeneratorgetInstance()Get the instancestatic RandomGeneratorgetInstance(java.security.SecureRandom random)Gets a secure random instancejava.lang.IntegergetIntegerRandom()Returns a random number as a Integerjava.lang.LonggetLongRandom()Returns a random number as a LongByteArraygetRandomByteArray(int size)Returns a random byte arrayjava.util.DategetRandomDateInFuture(long milisecondRange)Get a date in the futurejava.util.DategetRandomDateInPast(long milisecondRange)Get a date in the futurejava.util.DategetRandomDayInFuture(int dayRange)Get a date in the futurejava.util.DategetRandomDayInPast(int dayRange)Get a date in the futurelonggetRandomNumber(long range, boolean allowZero)Gets a random long numberjava.lang.StringgetRandomString(int size)Returns a random stringjava.lang.StringgetRandomString(int size, char[] validCharacters)Returns a random stringjava.security.SecureRandomgetSecureRandom()Returns the secure random instancebyte[]nextBytes(int size)Gets random bytes
-
-
-
Field Detail
-
validNumberCharacters
public static final char[] validNumberCharacters
Defines valid number characters
-
validLowerCaseLetterCharacters
public static final char[] validLowerCaseLetterCharacters
Defines valid lower case letter characters
-
validUpperCaseLetterCharacters
public static final char[] validUpperCaseLetterCharacters
Defines valid upper case letter characters
-
validLetterCharacters
public static final char[] validLetterCharacters
Defines valid letter characters
-
validNumberLetterCharacters
public static final char[] validNumberLetterCharacters
Defines valid number and letter characters
-
ONE_DAY
public static final long ONE_DAY
The miliseconds of a day- See Also:
- Constant Field Values
-
-
Method Detail
-
getInstance
public static RandomGenerator getInstance()
Get the instance- Returns:
- the instance
-
getInstance
public static RandomGenerator getInstance(java.security.SecureRandom random)
Gets a secure random instance- Parameters:
random- initialize the random generator with the given secure random instance.- Returns:
- the instance
-
getSecureRandom
public java.security.SecureRandom getSecureRandom()
Returns the secure random instance- Returns:
- secure random instance
-
getBooleanRandom
public java.lang.Boolean getBooleanRandom()
Returns a random number as a Boolean- Returns:
- random Boolean
-
getIntegerRandom
public java.lang.Integer getIntegerRandom()
Returns a random number as a Integer- Returns:
- random Integer
-
getLongRandom
public java.lang.Long getLongRandom()
Returns a random number as a Long- Returns:
- random Long
-
getFloatRandom
public java.lang.Float getFloatRandom()
Returns a random number as a Float- Returns:
- random Float
-
getDoubleRandom
public java.lang.Double getDoubleRandom()
Returns a random number as a Double- Returns:
- random Double
-
getRandomByteArray
public ByteArray getRandomByteArray(int size)
Returns a random byte array- Parameters:
size- the size of the byte array- Returns:
- random byte array
-
getRandomString
public java.lang.String getRandomString(int size)
Returns a random string- Parameters:
size- the size of the string- Returns:
- random string
-
getRandomString
public java.lang.String getRandomString(int size, char[] validCharacters)Returns a random string- Parameters:
size- the size of the stringvalidCharacters- the valid characters for the random string- Returns:
- random string
-
nextBytes
public byte[] nextBytes(int size)
Gets random bytes- Parameters:
size- the size of the random bytes- Returns:
- the instance
-
createGUID
public java.lang.String createGUID()
Generates a GUID: GUIDs are guaranteed to be globally unique by using ethernet MACs, IP addresses, time elements, and sequential numbers. GUIDs are not expected to be random and most often are easy/possible to guess given a sample from a given generator. GUIDs can be used as security devices to hide things such as files within a filesystem where listings are unavailable (e.g. files that are served up from a Web server with indexing turned off). This may be desireable in cases where standard authentication is not appropriate. In this scenario, the RandomGUIDs are used as directories. Another example is the use of GUIDs for primary keys in a database where you want to ensure that the keys are secret. Random GUIDs can then be used in a URL to prevent hackers (or users) from accessing records by guessing or simply by incrementing sequential numbers. There are many other possiblities of using GUIDs in the realm of security and encryption where the element of randomness is important. This method was written for these purposes but can also be used as a general purpose GUID generator as well. It generates truly random GUIDs by using the system's IP address (name/IP), system time in milliseconds (as an integer), and a very large random number joined together in a single String that is passed through an MD5 hash. The IP address and system time make the MD5 seed globally unique and the random number guarantees that the generated GUIDs will have no discernable pattern and cannot be guessed given any number of previously generated GUIDs. It is generally not possible to access the seed information (IP, time, random number) from the resulting GUIDs as the MD5 hash algorithm provides one way encryption.- Returns:
- a unique GUID
-
createUUID
public java.lang.String createUUID()
Creates a UUID- Returns:
- the uuid
-
getRandomDayInFuture
public java.util.Date getRandomDayInFuture(int dayRange)
Get a date in the future- Parameters:
dayRange- the range of days- Returns:
- the date
-
getRandomDateInFuture
public java.util.Date getRandomDateInFuture(long milisecondRange)
Get a date in the future- Parameters:
milisecondRange- the range in miliseconds- Returns:
- the date
-
getRandomDayInPast
public java.util.Date getRandomDayInPast(int dayRange)
Get a date in the future- Parameters:
dayRange- the range of days- Returns:
- the date
-
getRandomDateInPast
public java.util.Date getRandomDateInPast(long milisecondRange)
Get a date in the future- Parameters:
milisecondRange- the range in miliseconds- Returns:
- the date
-
getRandomNumber
public long getRandomNumber(long range, boolean allowZero)Gets a random long number- Parameters:
range- the range of the long. As example the range 5 means a random number between 1 and 5.allowZero- true if zero should also included into the range. As example the range 5 means a random number between 0 and 4.- Returns:
- the random number
-
chisQuare
public float chisQuare(int number, int size)Method to test the randomness of the random algorithm.- Parameters:
number- numbers of positiv numberssize- the numbers are less the size- Returns:
- the chis-quare. If chis-quare is in the range from 2*sqr(size) to size, then the algorithm has a good randomness in case the number is 10*size.
-
combineValidCharacters
public static char[] combineValidCharacters(char[] input, char[] input2)Helper methods to combines two character arrays- Parameters:
input- the inputinput2- the input two- Returns:
- the combined arrays
-
-