//Creates a Random instance with a seed of 12345. Random random = new Random(12345L); //Gets a ThreadLocalRandom instance ThreadLocalRandom tlr = ThreadLocalRandom.current(); //Set the instance's seed. tlr.setSeed(12345L);
Using the same seed to generate random numbers will return the same numbers every time, so setting a different seed for every Random instance is a good idea if you don't want to end up with duplicate numbers
A good method to get a Long that is different for every call is System.currentTimeMillis():
Random random = new Random(System.currentTimeMillis()); ThreadLocalRandom.current().setSeed(System.currentTimeMillis());
/** * returns a array of random numbers with no duplicates * @param range the range of possible numbers for ex. if 100 then it can be anywhere from 1-100 * @param length the length of the array of random numbers * @return array of random numbers with no duplicates. */ public static int[] getRandomNumbersWithNoDuplicates(int range, int length){ if (length<range){ // this is where all the random numbers int[] randomNumbers = new int[length]; // loop through all the random numbers to set them for (int q = 0; q < randomNumbers.length; q++){ // get the remaining possible numbers int remainingNumbers = range-q; // get a new random number from the remainingNumbers int newRandSpot = (int) (Math.random()*remainingNumbers); newRandSpot++; // loop through all the possible numbers for (int t = 1; t < range+1; t++){ // check to see if this number has already been taken boolean taken = false; for (int number : randomNumbers){ if (t==number){ taken = true; break; } } // if it hasnt been taken then remove one from the spots if (!taken){ newRandSpot--; // if we have gone though all the spots then set the value if (newRandSpot==0){ randomNumbers[q] = t; } } } } return randomNumbers; } else { // invalid can't have a length larger then the range of possible numbers } return null; }
The method works by looping though an array that has the size of the requested length and finds the remaining length of possible numbers. It sets a random number of those possible numbers newRandSpot and finds that number within the non taken number left. It does this by looping through the range and checking to see if that number has already been taken.
For example if the range is 5 and the length is 3 and we have already chosen the number 2. Then we have 4 remaining numbers so we get a random number between 1 and 4 and we loop through the range(5) skipping over any numbers that we have already used(2).
Now let's say the next number chosen between 1 & 4 is 3. On the first loop we get 1 which has not yet been taken so we can remove 1 from 3 making it 2. Now on the second loop we get 2 which has been taken so we do nothing. We follow this pattern until we get to 4 where once we remove 1 it becomes 0 so we set the new randomNumber to 4.
We can use org.apache.commons.lang3.RandomUtils to generate random numbers using a single line.
int x = RandomUtils.nextInt(1, 1000);
The method nextInt(int startInclusive, int endExclusive) takes a range.
Apart from int, we can generate random long, double, float and bytes using this class.
RandomUtils class contains the following methods
static byte[] nextBytes(int count) //Creates an array of random bytes. static double nextDouble() //Returns a random double within 0 - Double.MAX_VALUE static double nextDouble(double startInclusive, double endInclusive) //Returns a random double within the specified range. static float nextFloat() //Returns a random float within 0 - Float.MAX_VALUE static float nextFloat(float startInclusive, float endInclusive) //Returns a random float within the specified range. static int nextInt() //Returns a random int within 0 - Integer.MAX_VALUE static int nextInt(int startInclusive, int endExclusive) //Returns a random integer within the specified range. static long nextLong() //Returns a random long within 0 - Long.MAX_VALUE static long nextLong(long startInclusive, long endExclusive) //Returns a random long within the specified range.
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions