Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7555065
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T11:30:17+00:00 2026-05-30T11:30:17+00:00

I was wondering what may be the reason to use this median function, instead

  • 0

I was wondering what may be the reason to use this median function, instead of just calculating the min + (max - min) / 2:

// used by the random number generator
private static final double  M_E12 = 162754.79141900392083592475;

/**
 * Return an estimate of median of n values distributed in [min,max)
 * @param min the minimum value 
 * @param max the maximum value
 * @param n 
 * @return an estimate of median of n values distributed in [min,max)
 **/
private static double median(double min, double max, int n) 
{
    // get random value in [0.0, 1.0)
    double t = (new Random()).nextDouble();

    double retval;
    if (t > 0.5) {
        retval = java.lang.Math.log(1.0-(2.0*(M_E12-1)*(t-0.5)/M_E12))/12.0;
    } else {
        retval = -java.lang.Math.log(1.0-(2.0*(M_E12-1)*t/M_E12))/12.0;
    }
    // We now have something distributed on (-1.0,1.0)
    retval = (retval+1.0) * (max-min)/2.0;
    retval = retval + min;
    return retval;
}

The only downside of my approach would maybe be its deterministic nature, I’d say?

The whole code can be found here, http://www.koders.com/java/fid42BB059926626852A0D146D54F7D66D7D2D5A28D.aspx?s=cdef%3atree#L8, btw.

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T11:30:18+00:00Added an answer on May 30, 2026 at 11:30 am

    [trying to cover a range here because it’s not clear to me what you’re not understanding]

    first, the median is the middle value. the median of [0,0,1,99,99] is 1.

    and so we can see that the code given is not calculating the median (it’s not finding a middle value). instead, it’s estimating it from some theoretical distribution. as the comment says.

    the forumla you give is for the mid-point. if many values are uniformly distributed between min and max then yes, that is a good estimation of the median. in this case (presumably) the values are not distributed in that way and so some other method is necessary.

    you can see why this could be necessary by calculating the mid point of the numbers above – your formula would give 49.5.

    the reason for using an estimate is probably that it is much faster than finding the median. the reason for making that estimate random is likely to avoid a bad worst case on multiple calls.

    and finally, sorry but i don’t know what the distribution is in this case. you probably need to search for the data structure and/or author name to see if you can find a paper or book reference (i thought it might be assuming a power law, but see edit below – it seems to be adding a very small correction) (i’m not sure if that is what you are asking, or if you are more generally confused).

    [edit] looking some more, i think the log(…) is giving a central bias to the uniformly random t. so it’s basically doing what you suggest, but with some spread around the 0.5. here’s a plot of one case which shows that retval is actually a pretty small adjustment.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This may seem like a daft question, but i was wondering about how to
This may boil down to opinion: I am wondering if project files (the files
I may want to create a RIA but am wondering, whether Adobe AIR or
Wondering if anyone can help me with this annoying but trivial (in terms of
Wondering if this is possible. We have an 3rd Party library that contains identification
This is a really weird one and I'm wondering if I need to change
I am almost sure that this is not possible, but I'm wondering if someone
I understand the use of CanExecute() and Execute() , but I'm wondering about the
I know it may seem kind of strange but I was wondering if anybody
I've been wondering why would I use Exceptions in my PHP. Let's take a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.