About 22,300 results
Open links in new tab
  1. java - How to randomly pick an element from an array - Stack Overflow

    Apr 23, 2015 · use java.util.Random to generate a random number between 0 and array length: random_number, and then use the random number to get the integer: array[random_number]

  2. java - Fill an array with random numbers - Stack Overflow

    You can use ThreadLocalRandom with the Random#doubles method to generate a certain number of random doubles. import java.util.concurrent.ThreadLocalRandom; //... public static double[] list() { return anArray = ThreadLocalRandom.current().doubles(10).toArray(); //Generate 10 random doubles } To generate random doubles within a certain range ...

  3. How to Add Random Number to an Array in Java?

    Nov 18, 2024 · To generate an array of integers with random values the nextInt () method from the java.util.Random class is used. From the random number generator sequence, this method returns the next random integer value. We can assign random values …

  4. How to Fill an Array With Random Numbers - Baeldung

    Aug 13, 2024 · In this article, we’ve explored various ways to fill an array with random numbers using random number generators in Java. Each pseudo-random number generator class has its advantages and disadvantages.

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

  6. Generating Random Numbers in Java - GeeksforGeeks

    Apr 24, 2025 · There are multiple ways to generate random numbers using built-in methods and classes in Java. The most commonly used approaches are listed below: java.util.Random class; Math.random() method (returns double values) ThreadLocalRandom class (introduced in Java 7) Let’s explore each of these approaches one by one in detail. 1. Using java.util ...

  7. Generate Random Number from an Array in Java - Online …

    In this article, we will learn how to generate a random number from an array of integers in Java by using Random class. The Random class provides methods to generate random numbers, and we will use the nextInt (int bound) method to get a random index within the bounds of …

  8. Generate a Random Array of Integers in Java - Online Tutorials …

    Learn how to generate a random array of integers in Java with this comprehensive guide, including code examples and explanations. Master the technique of generating a random array of integers in Java through our detailed tutorial with practical examples.

  9. How to Generate Array Of Random Numbers In Java

    In this instructional exercise, we will learn how to generate the array of random numbers in Java using simple java code. If you don’t know how to generate random numbers, then you are in the right place to search for your solution.

  10. 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;

  11. Some results have been removed
Refresh