2 Replies - 1824 Views - Last Post: 14 March 2012 - 03:54 AM Rate Topic: -----

#1 Dil92   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 14-March 12

What's the different between Math.Random and Random class in java?

Posted 14 March 2012 - 03:22 AM

Can anyone please inform me what's the true different between Math.random() method and the Random prepackaged class in Java libraries. I need to know how to use them separately.
Is This A Good Question/Topic? 0
  • +

Replies To: What's the different between Math.Random and Random class in java?

#2 Sheph   User is offline

  • D.I.C Lover
  • member icon

Reputation: 447
  • View blog
  • Posts: 1,032
  • Joined: 12-October 11

Re: What's the different between Math.Random and Random class in java?

Posted 14 March 2012 - 03:45 AM

We can always check the API documentation. The docs for Math.random() says that it uses a new java.util.Random() object to get its values. That Random object is used only in the Math class by nothing else. It is essentially the same as doing it yourself:

Random r = new Random();
r.nextDouble();
Was This Post Helpful? 1
  • +
  • -

#3 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: What's the different between Math.Random and Random class in java?

Posted 14 March 2012 - 03:54 AM

Nothimg better than going to the source :)

From: \java\lang\Math.java
    private static Random randomNumberGenerator;

    private static synchronized void initRNG() {
        if (randomNumberGenerator == null) 
            randomNumberGenerator = new Random();
    }

    /**
     * Returns a <code>double</code> value with a positive sign, greater 
     * than or equal to <code>0.0</code> and less than <code>1.0</code>. 
     * Returned values are chosen pseudorandomly with (approximately) 
     * uniform distribution from that range. 
     * 
     * <p>When this method is first called, it creates a single new
     * pseudorandom-number generator, exactly as if by the expression
     * <blockquote><pre>new java.util.Random</pre></blockquote> This
     * new pseudorandom-number generator is used thereafter for all
     * calls to this method and is used nowhere else.
     * 
     * <p>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.
     *  
     * @return  a pseudorandom <code>double</code> greater than or equal 
     * to <code>0.0</code> and less than <code>1.0</code>.
     * @see     java.util.Random#nextDouble()
     */
    public static double random() {
        if (randomNumberGenerator == null) initRNG();
        return randomNumberGenerator.nextDouble();
    }



Was This Post Helpful? 1
  • +
  • -

Page 1 of 1