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

  • Home
  • SEARCH
  • 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 1105353
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T01:37:10+00:00 2026-05-17T01:37:10+00:00

I am attempting to generate a random numbers with a logarithmic distribution. Where n=1

  • 0

I am attempting to generate a random numbers with a logarithmic distribution.

Where n=1 occurs half of the time, n=2 occurs a quarter of the time, n=3 occurs an eighth of the time, etc.

    int maxN = 5;
    int t = 1 << (maxN); // 2^maxN
    int n = maxN -
            ((int) (Math.log((Math.random() * t))
            / Math.log(2))); // maxN - log2(1..maxN)
    System.out.println("n=" + n);

Most of the time, I am getting the result I need, however once every so often, I get a value of n that is larger than maxN.

Why is this so? The way I see it, the max value of Math.random() is 1.0;
therefore the max value of (Math.random() * t)) is t;
therefore the max value of log2(t) is maxN, since t = 2^maxN;

Where has my logic gone off track?

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-17T01:37:11+00:00Added an answer on May 17, 2026 at 1:37 am

    logarithm of numbers less than 1.0 is negative. When the random number generated is such that it is less than 1.0, the expression ((int) (Math.log(Math.random() * t) / Math.log(2))) is a negative number and hence maxN - (the negative number) is more than maxN.

    The following expression should give correct distribution.

    n = Math.floor(Math.log((Math.random() * t) + 1)/Math.log(2))
    

    Note that:

    0.0 <= Math.random() <= 1.0
    0.0 <= Math.random() * t <= t
    1.0 <= (Math.random() * t) + 1 <= t + 1.0
    0.0 <= Math.log((Math.random() * t) + 1) <= Math.log(t + 1.0)
    0.0 <= Math.log((Math.random() * t) + 1)/Math.log(2) <= Math.log(t + 1.0)/Math.log(2)
    
    Since t = 2^maxN,
    Math.log(t + 1.0)/Math.log(2) is slightly larger than maxN.
    
    So do a Math.floor and you get the correct result:
    0.0 <= Math.floor(Math.log((Math.random() * t) + 1)/Math.log(2)) <= maxN
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm attempting to generate infinite random terrain. The terrain should generate the same every
I am attempting to generate datamatrix barcodes from within itext. This works fine for
I am attempting to generate a BitmapFrame that is based on a UIElement .
I am attempting to generate an Expression tree that ultimately calls a series of
I am attempting to generate a list (from four tables) of products, classid, optionid,
I am attempting to use resources to auto generate routes for my resource. The
I am attempting to generate the body of an email in Outlook 2007 from
I'm attempting to generate an SVN log file through TortoiseSVN, if that is even
I'm attempting to implement a random number generator system; essentially I'm reading in an
I'm attempting to generate a grid of divs five elements wide from the objects

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.