ofbiz-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Michael Brohl (Jira)" <j...@apache.org>
Subject [jira] [Commented] (OFBIZ-11020) Use Random.nextDouble() instead of Math.random()
Date Sat, 22 Feb 2020 11:20:00 GMT

    [ https://issues.apache.org/jira/browse/OFBIZ-11020?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17042525#comment-17042525
] 

Michael Brohl commented on OFBIZ-11020:
---------------------------------------

Due to the implementation docs the RandomNumberGenerator for Math.random() is created once
an reused/properly synchronized.
{code:java}
    /**
     * Returns a {@code double} value with a positive sign, greater
     * than or equal to {@code 0.0} and less than {@code 1.0}.
     * 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>{@code new java.util.Random()}</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} greater than or equal
     * to {@code 0.0} and less than {@code 1.0}.
     * @see Random#nextDouble()
     */
    public static double random() {
        return RandomNumberGeneratorHolder.randomNumberGenerator.nextDouble();
    }
{code}
I see no real benefit from changing this to new Random() ... then.

Closing.

> Use Random.nextDouble() instead of Math.random()
> ------------------------------------------------
>
>                 Key: OFBIZ-11020
>                 URL: https://issues.apache.org/jira/browse/OFBIZ-11020
>             Project: OFBiz
>          Issue Type: Improvement
>          Components: framework
>            Reporter: bd2019us
>            Assignee: Michael Brohl
>            Priority: Trivial
>         Attachments: OFBIZ-11020.patch
>
>
> When using Math.random instead of Random.nextDouble, there is a slight performance cost.
Due to Math.random invoking an instance of Random This cost can be alleviated by directly using
Random.nextDouble. This also allows for more control over the randomization in the future.



--
This message was sent by Atlassian Jira
(v8.3.4#803005)

Mime
View raw message