-2

Simple question

java.lang.Math.random()

How does this work? Meaning there is no seed input, so does it generate a random number off the system time? Meaning like if two calls were made to this function at .00001s away from eachother (basically the same time), would it produce the same result?

Thanks!

2

4 Answers 4

3

The javadoc explains how it works:

When this method is first called, it creates a single new pseudorandom-number generator, exactly as if by the expression

new java.util.Random()

This new pseudorandom-number generator is used thereafter for all calls to this method and is used nowhere else.

2

Returned values are chosen pseudorandomly with (approximately) uniform distribution from that range. When this method is first called, it creates a single new pseudorandom-number generator, exactly as if by the expression new java.util.Random

This new pseudorandom-number generator is used thereafter for all calls to this method and is used nowhere else. This method is properly synchronized to allow correct use by more than one thread. However, if many threads need to generate pseudorandom numbers at a great rate, it may reduce contention for each thread to have its own pseudorandom-number generator.

1

In order to understand how does this code runs you must go through the various Random Number generator algorithms. In actual practice theres no concept call random numbers if you google "Psuedo Random Number Algorithm" then you can have a better insight about the various concepts. Answering your Question : Yes there will be different if the Random Number Generator Algorithm is based on time (usually they are). But at the output if u write

Random obj1 = new Random()
int p = obj1.nextInt(10%2)
int q = obj1.nextGaussian();

theres a chance that the same number may appear more than once. It is because the Number generated is undoubtedly a unique number but it due to various parameters the obtained output is filtered and so theres a probabilty that the ouput can be same

0

There are two principal means of generating random (really pseudo-random) numbers: the Random class generates random integers, doubles, longs and so on, in various ranges. the static method Math.random generates doubles between 0 (inclusive) and 1 (exclusive). To generate random integers: do not use Math.random (it produces doubles, not integers) use the Random class to generate random integers between 0 and N. To generate a series of random numbers as a unit, you need to use a single Random object - do not create a new Random object for each new random number.

Other alternatives are:

SecureRandom, a cryptographically strong subclass of Random ThreadLocalRandom, intended for multi-threaded cases

import java.util.Random;

/** Generate 10 random integers in the range 0..99. */

public final class RandomInteger {

  public static final void main(String... aArgs){
    log("Generating 10 random integers in range 0..99.");

//note a single Random object is reused here

            Random randomGenerator = new Random();
    for (int idx = 1; idx <= 10; ++idx){
      int randomInt = randomGenerator.nextInt(100);
      log("Generated : " + randomInt);
    }

    log("Done.");
  }

  private static void log(String aMessage){
    System.out.println(aMessage);
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.