About 237,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; or

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

    To generate a random number "in between two numbers", use the following code: Random r = new Random(); int lowerBound = 1; int upperBound = 11; int result = r.nextInt(upperBound-lowerBound) + lowerBound; This gives you a random number in between 1 (inclusive) and 11 (exclusive), so initialize the upperBound value by adding 1.

  3. Java random number with given length - Stack Overflow

    Mar 22, 2011 · To generate a 6-digit number: Use Random and nextInt as follows: Random rnd = new Random(); int n = 100000 + rnd.nextInt(900000); Note that n will never be 7 digits (1000000) since nextInt(900000) can at most return 899999. So how do I randomize the last 5 chars that can be either A-Z or 0-9? Here's a simple solution:

  4. Generating Unique Random Numbers in Java - Stack Overflow

    This is the most simple method to generate unique random values in a range or from an array. In this example, I will be using a predefined array but you can adapt this method to generate random numbers as well. First, we will create a sample array to retrieve our data from. Generate a random number and add it to the new array.

  5. 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)...

  6. java - generate random number with restrictions - Stack Overflow

    This says, generate a random number between 0 and 6. Add 1 to that number to ensure that it can never be 0. So if it picks 0, +1 is added making it 1. If it picks 6, +1 is added making it 7, etc. This satisfies rule #1 and rule #2. You ensure the first number never has an 8 or 9. (rand.nextInt(8) * 10) + rand.nextInt(8) Genreate a random number ...

  7. Java: random long number in 0 <= x < n range - Stack Overflow

    Random class has a method to generate random int in a given range. For example: Random r = new Random(); int x = r.nextInt(100); This would generate an int number more or equal to 0 and less than 100. I'd like to do exactly the same with long number. long y = magicRandomLongGenerator(100);

  8. How do I generate a random integer between min and max in Java?

    @ataulm: In older versions of Java creating multiple Random objects didn't guarantee randomness. "Two Random objects created within the same millisecond will have the same sequence of random numbers." . In newer versions it is probably OK, but I didn't know that when I wrote my comment three years ago. –

  9. java - Generate 6 digit random number - Stack Overflow

    Jul 13, 2018 · this will return your number in string format, so the "0" will be "000000". Here is the code. public static String getRandomNumberString() { // It will generate 6 digit random Number. // from 0 to 999999 Random rnd = new Random(); int number = rnd.nextInt(999999); // this will convert any number sequence into 6 character.

  10. java - What is the best way to generate a random float value …

    For a random value within a range, the formula is: double random = min + Math.random() * (max - min); This basic formula is constant no matter what you use to generate random numbers. Math.random() provides moderately well distributed numbers, but you can replace it with whatever random number generator you want, for example (slightly better):

Refresh