commons-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Sebb (JIRA)" <j...@apache.org>
Subject [jira] Commented: (LANG-472) RandomUtils.nextLong() get all even number
Date Thu, 04 Feb 2010 04:24:27 GMT

    [ https://issues.apache.org/jira/browse/LANG-472?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12829431#action_12829431
] 

Sebb commented on LANG-472:
---------------------------

[Actually it was Phil's patch, amended to use the shared static Random instance.]

One could also use Phil's solution and put:

{code}
if (n == Long.MAX_VALUE) {
    return Math.abs(SHARED_RANDOM.nextLong());
}
{code}

However, that only fixes the one value - as far as I can tell, nextLong(long) starts failing
tests at around Long.MAX_VALUE/920 (not that there are any tests in SVN yet!)

BTW, I think your hack would be more efficient as

{code}
if (n == Long.MAX_VALUE) {
    double random = -1;
    while (random < 0) {
        random = Math.random();
    }
    return new BigDecimal(random).movePointRight(19).longValue();
}
{code}

> RandomUtils.nextLong() get all even number
> ------------------------------------------
>
>                 Key: LANG-472
>                 URL: https://issues.apache.org/jira/browse/LANG-472
>             Project: Commons Lang
>          Issue Type: Bug
>          Components: lang.math.*
>         Environment: all system
>            Reporter: zhangruimin
>             Fix For: 2.x
>
>         Attachments: LANG-472-alt.patch, lang-472.patch
>
>
> when we use the following code , we can see that the method produce only even number.
>          while (true) {
> //        for (int i = 0; i < 100; i++) {
>             if (RandomUtils.nextLong() % 2 == 1) {
>                 System.out.println("ok");
>             }
>         }

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


Mime
View raw message