About 481,000 results
Open links in new tab
  1. Random generator giving me the same number everytime

    Dec 16, 2012 · Right now you are initializing the random number generator with the same seed. This will make it produce the same sequence of numbers every time. You want to be using the no-arg constructor: Random gen = new Random(); According to the documentation

  2. Generating random numbers in Java - GeeksforGeeks

    Jan 4, 2025 · Java provides three ways to generate random numbers using some built-in methods and classes as listed below: java.util.Random class; Math.random method : Can Generate Random Numbers of double type. ThreadLocalRandom class; 1) java.util.Random

  3. Java random always returns the same number when I set the seed?

    To fix that, move the randnum local variable instance of Random from the scope of the random instance method to the class scope. Second, set the seed only when random is assigned a Random instance or only to get same sequence of results from it to start over again.

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

  5. Generating Random Numbers in Java (with Thread Safety)

    Sep 6, 2023 · Learn to use java.util.Random, Math.random(), SecureRandom and ThreadLocalRandom to generate random numbers based on your application requirements.

  6. Creating Random Numbers With No Duplicates in Java

    Jan 8, 2024 · In this quick tutorial, we’ll learn how to generate random numbers with no duplicates using core Java classes. First, we’ll implement a couple of solutions from scratch, then take advantage of Java 8+ features for a more extensible approach. 2. Random Numbers From a …

  7. Generating Random Numbers in Java - HappyCoders.eu

    Nov 27, 2024 · Experienced Java developers familiar with the various ways to create random values can skip directly to the "Pseudorandom Number Generation" or "Changes in Implementations Over Time" section. You can find the code samples for …

    • Reviews: 17
    • Random Number Generators in Java - Baeldung

      Jan 8, 2024 · Java 17 provides a large number of improvements to the generation of random numbers. We explore the new API and compare it with the original random number generators.

    • Java Random Number Generator – How to Generate Integers With Math Random

      Nov 25, 2020 · In this article, we will learn how to generate pseudo-random numbers using Math.random() in Java. 1. Use Math.random() to Generate Integers. Math.random() returns a double type pseudo-random number, greater than or equal to zero and less than one. Let's try it out with some code:

    • Better Random Number Generation in Java 17 - nipafx // You. Me. Java.

      Sep 13, 2021 · Java 17 expands the API for random number generation to make it more usable, extensible, and robust with RandomGenerator and RandomGeneratorFactory at its core. Java's API around random numbers used to be a bit muddied. First and foremost, there's the class Random, which has a solid API.

    • Some results have been removed
    Refresh