About 7,220,000 results
Open links in new tab
  1. Getting random numbers in Java - Stack Overflow

    May 5, 2011 · The first solution is to use the java.util.Random class: import java.util.Random; Random rand = new Random(); // Obtain a number between [0 - 49]. int n = rand.nextInt(50); // Add 1 to the result to get a number from the required range // (i.e., [1 - 50]). n += 1; Another solution is using Math.random(): double random = Math.random() * 49 + 1 ...

  2. How do I generate random integers within a specific range in Java ...

    Min + (int)(Math.random() * ((Max - Min) + 1)) The Java Math library function Math.random() generates a double value in the range [0,1). Notice this range does not include the 1. In order to get a specific range of values first, you need to multiply by the magnitude of the range of values you want covered. Math.random() * ( Max - Min )

  3. Generating a Random Number between 1 and 10 Java

    Random rand = new Random(); // nextInt as provided by Random is exclusive of the top value so you need to add 1 int randomNum = rand.nextInt((max - min) + 1) + min; See the relevant JavaDoc . As explained by Aurund, Random objects created within a short time of each other will tend to produce similar output, so it would be a good idea to keep ...

  4. How Java random generator works? - Stack Overflow

    Feb 17, 2016 · Check How does java.util.Random work and how good is it?: In other words, we begin with some start or "seed" number which ideally is "genuinely unpredictable", and which in practice is "unpredictable enough". For example, the number of milliseconds— or even nanoseconds— since the computer was switched on is available on most systems.

  5. Generating Unique Random Numbers in Java - Stack Overflow

    Jun 18, 2016 · With Java 8+ you can use the ints method of Random to get an IntStream of random values then distinct and limit to reduce the stream to a number of unique random values. ThreadLocalRandom.current().ints(0, 100).distinct().limit(5).forEach(System.out::println);

  6. Java generating non-repeating random numbers - Stack Overflow

    Get Random number Returns a random integer x satisfying lower <= x <= upper. If lower > upper, returns 0. @param lower @param upper @return. In the main method I created list then i check if the random number exist on the list if it doesn't exist i will add the random number to the list. It is very slow but straight forward.

  7. Java random numbers using a seed - Stack Overflow

    Apr 28, 2018 · This is my code to generate random numbers using a seed as an argument: double randomGenerator(long seed) { Random generator = new Random(seed); double num = generator.nextDouble() * (0.5)...

  8. Is there functionality to generate a random character in Java?

    Apr 13, 2010 · java.util.Random is the more effective one I have tried out so far, having a precision of 98.65% uniqueness. I have provided bellow some tests which generate 10000 batches of a hundred 2 alphanumeric chars strings and calculates the average. Other random tools were RandomStringUtils from commons.lang3 and java.util.Math.

  9. java - How to randomly pick an element from an array - Stack …

    Apr 23, 2015 · With Java 7, one can use ThreadLocalRandom.. A random number generator isolated to the current thread. Like the global Random generator used by the Math class, a ThreadLocalRandom is initialized with an internally generated seed …

  10. java - Generate a random double in a range - Stack Overflow

    This question was asked before Java 7 release but now, there is another possible way using Java 7 (and above) API: double random = ThreadLocalRandom.current().nextDouble(min, max); nextDouble will return a pseudorandom double value between the minimum (inclusive) and the maximum (exclusive). The bounds are not necessarily int, and can be double.

Refresh