Package com.github.toolarium.common.util
Class RandomGenerator
java.lang.Object
com.github.toolarium.common.util.RandomGenerator
This class implements some helper methods for random numbers. It works internal with the SecureRandom class.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final longThe miliseconds of a daystatic final char[]Defines valid letter charactersstatic final char[]Defines valid lower case letter charactersstatic final char[]Defines valid number charactersstatic final char[]Defines valid number and letter charactersstatic final char[]Defines valid upper case letter characters -
Method Summary
Modifier and TypeMethodDescriptionfloatchisQuare(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 arraysGenerates a GUID: GUIDs are guaranteed to be globally unique by using ethernet MACs, IP addresses, time elements, and sequential numbers.Creates a UUIDReturns a random number as a BooleanReturns a random number as a DoubleReturns a random number as a Floatstatic RandomGeneratorGet the instancestatic RandomGeneratorgetInstance(SecureRandom random) Gets a secure random instanceReturns a random number as a IntegerReturns a random number as a LonggetRandomByteArray(int size) Returns a random byte arraygetRandomDateInFuture(long milisecondRange) Get a date in the futuregetRandomDateInPast(long milisecondRange) Get a date in the futuregetRandomDayInFuture(int dayRange) Get a date in the futuregetRandomDayInPast(int dayRange) Get a date in the futurelonggetRandomNumber(long range, boolean allowZero) Gets a random long numbergetRandomString(int size) Returns a random stringgetRandomString(int size, char[] validCharacters) Returns a random stringReturns the secure random instancebyte[]nextBytes(int size) Gets random bytes
-
Field Details
-
validNumberCharacters
public static final char[] validNumberCharactersDefines valid number characters -
validLowerCaseLetterCharacters
public static final char[] validLowerCaseLetterCharactersDefines valid lower case letter characters -
validUpperCaseLetterCharacters
public static final char[] validUpperCaseLetterCharactersDefines valid upper case letter characters -
validLetterCharacters
public static final char[] validLetterCharactersDefines valid letter characters -
validNumberLetterCharacters
public static final char[] validNumberLetterCharactersDefines valid number and letter characters -
ONE_DAY
public static final long ONE_DAYThe miliseconds of a day- See Also:
-
-
Method Details
-
getInstance
Get the instance- Returns:
- the instance
-
getInstance
Gets a secure random instance- Parameters:
random- initialize the random generator with the given secure random instance.- Returns:
- the instance
-
getSecureRandom
Returns the secure random instance- Returns:
- secure random instance
-
getBooleanRandom
Returns a random number as a Boolean- Returns:
- random Boolean
-
getIntegerRandom
Returns a random number as a Integer- Returns:
- random Integer
-
getLongRandom
Returns a random number as a Long- Returns:
- random Long
-
getFloatRandom
Returns a random number as a Float- Returns:
- random Float
-
getDoubleRandom
Returns a random number as a Double- Returns:
- random Double
-
getRandomByteArray
Returns a random byte array- Parameters:
size- the size of the byte array- Returns:
- random byte array
-
getRandomString
Returns a random string- Parameters:
size- the size of the string- Returns:
- random string
-
getRandomString
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
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
Creates a UUID- Returns:
- the uuid
-
getRandomDayInFuture
Get a date in the future- Parameters:
dayRange- the range of days- Returns:
- the date
-
getRandomDateInFuture
Get a date in the future- Parameters:
milisecondRange- the range in miliseconds- Returns:
- the date
-
getRandomDayInPast
Get a date in the future- Parameters:
dayRange- the range of days- Returns:
- the date
-
getRandomDateInPast
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
-