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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T06:20:41+00:00 2026-05-24T06:20:41+00:00

I need to generate n random numbers between a and b , but any

  • 0

I need to generate n random numbers between a and b, but any two numbers cannot have a difference of less than c. All variables except n are floats (n is an int).

Solutions are preferred in java, but C/C++ is okay too.

Here is what code I have so far.:

static float getRandomNumberInRange(float min, float max) {
    return (float) (min + (Math.random() * (max - min)));
}

static float[] randomNums(float a, float b, float c, int n) {
    float minDistance = c;
    float maxDistance = (b - a) - (n - 1) * c;
    float[] randomNumArray = new float[n];
    float random = getRandomNumberInRange(minDistance, maxDistance);
    randomNumArray[0] = a + random;
    for (int x = 1; x < n; x++) {
        maxDistance = (b - a) - (randomNumArray[x - 1]) - (n - x - 1) * c;
        random = getRandomNumberInRange(minDistance, maxDistance);
        randomNumArray[x] = randomNumArray[x - 1] + random;
    }
    return randomNumArray;
}

If I run the function as such (10 times), I get the following output:

Input: randomNums(-1f, 1f, 0.1f, 10)

[-0.88, 0.85, 1.23, 1.3784, 1.49, 1.59, 1.69, 1.79, 1.89, 1.99]

[-0.73, -0.40, 0.17, 0.98, 1.47, 1.58, 1.69, 1.79, 1.89, 1.99]

[-0.49, 0.29, 0.54, 0.77, 1.09, 1.56, 1.69, 1.79, 1.89, 1.99]

  • 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-24T06:20:42+00:00Added an answer on May 24, 2026 at 6:20 am

    I think a reasonable approach can be the following:

    schema

    1. Total “space” is (b - a)
    2. Remove the minimum required space (n-1)*c to obtain the remaining space
    3. Shot (n-1) random numbers between 0 and 1 and scale them so that the sum is this just computed “optional space”. Each of them will be a “slice” of space to be used.
    4. First number is a
    5. For each other number add c and the next “slice” to the previous number. Last number will be b.

    If you don’t want first and last to match a and b exactly then just create n+1 slices instead of n-1 and start with a+slice[0] instead of a.

    The main idea is that once you remove the required spacing between the points (totalling (n-1)*c) the problem is just to find n-1 values so that the sum is the prescribed “optional space”. To do this with a uniform distribution just shoot n-1 numbers, compute the sum and uniformly scale those numbers so that the sum is instead what you want by multiplying each of them by the constant factor k = wanted_sum / current_sum.

    To obtain the final result you just use as spacing between a value and the previous one the sum of the mandatory part c and one of the randomly sampled variable parts.

    An example in Python of the code needed for the computation is the following

    space = b - a
    slack = space - (n - 1)*c
    slice = [random.random() for i in xrange(n-1)]  # Pick (n-1) random numbers 0..1
    k = slack / sum(slice)                          # Compute needed scaling
    slice = [x*k for x in slice]                    # Scale to get slice sizes
    result = [a]
    for i in xrange(n-1):
        result.append(result[-1] + slice[i] + c)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Like my question, i need to generate random numbers that have identical pairs between
I need to generate a vector of random float numbers between [0,1] such that
I need to create a function that will generate 2 random numbers between x
I know how to generate random numbers, but what I really need is a
I have an interval consisting of two float numbers, and need to generate 20
A specific example I need to generate a random number between 0 and 2,
I need to generate a random port number between 2000-65000 from a shell script.
I need to generate a random integer between 1 and n (where n is
For a simple simulation in C, I need to generate exponential random variables. I
I need to generate a series of N random binary variables with a given

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.