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 8798067
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T23:59:38+00:00 2026-06-13T23:59:38+00:00

Possible Duplicate: Generate random number with non-uniform density I try to identify/create a function

  • 0

Possible Duplicate:
Generate random number with non-uniform density

I try to identify/create a function ( in Java ) that give me a nonuniform distributed sequence of number.
if I has a function that say function f(x), and x>0 it will give me a random number
from 0 to x.

The function most work with any given x and this below is only a example how I want to have.

But if we say x=100 the function f(x) will return s nonunifrom distributed.

And I want for example say

0 to 20 be approximately 20% of all case.

21 to 50 be approximately 50% of all case.

51 to 70 be approximately 20% of all case.

71 to 100be approximately 10 of all case.

In short somting that give me a number like normal distribution and it peek at 30-40 in this case x is 100.

http://en.wikipedia.org/wiki/Normal_distribution

( I can use a uniform random gen as score if need, and only a function that will transfrom the uniform result to a non-uniform result. )

EDIT

My final solution for this problem is:

/**
 * Return a value from [0,1] and mean as 0.3, It give 10% of it is lower
 * then 0.1. 5% is higher then 0.8 and 30% is in rang 0.25 to 0.45
 *
 * @return
 */
public double nextMyGaussian() {
    double d = -1000;
    while (d < -1.5) {
        // RANDOMis Java's normal Random() class.
        // The nextGaussian is normal give a value from -5 to +5?
        d = RANDOM.nextGaussian() * 1.5;
    }
    if (d > 3.5d) {
        return 1;
    }
    return ((d + 1.5) / 5);
}
  • 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-06-13T23:59:39+00:00Added an answer on June 13, 2026 at 11:59 pm

    Could you just write a function that sums a number of random numbers it the 1-X range and takes an average? this will tend to the normal distribution as n increases

    See:

    Generate random numbers following a normal distribution in C/C++

    I hacked something like the below:

    class CrudeDistribution {
    
        final int TRIALS = 20;
    
        public int getAverageFromDistribution(int upperLimit) {
            return getAverageOfRandomTrials(TRIALS, upperLimit);
        }
    
        private int getAverageOfRandomTrials(int trials, int upperLimit) {
            double d = 0.0;
            for (int i=0; i<trials; i++) {
                d +=getRandom(upperLimit);
            }
            return (int) (d /= trials);
        }
    
        private int getRandom(int upperLimit) {
            return (int) (Math.random()*upperLimit)+1;
        }
    }
    

    There are libraries in Commons-Math that can generate distributions based on means and standard deviations (that measure the spread). and in the link some algorithms that do this.

    Probably a fun hour of so of hunting to find the relevant 2 liner:

    https://commons.apache.org/math/userguide/distribution.html

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

Sidebar

Related Questions

Possible Duplicate: Java: generating random number in a range I want to generate a
Possible Duplicate: Java: generating random number in a range How do I generate a
Possible Duplicate: Generating random number in a range with Java How can I generate
Possible Duplicate: Java: generating random number in a range I want to generate random
Possible Duplicate: Java: generating random number in a range How do I generate a
Possible Duplicate: Algorithm for generating a random number is posible to generate a random
Possible Duplicate: How to generate a random number from within a range - C
Possible Duplicate: How to generate a random number from within a range - C
Possible Duplicate: Generating Random Numbers in Objective-C How do I generate a random number
Possible Duplicate: Generating random number in a range with Java This feels pretty silly,

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.