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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T03:09:43+00:00 2026-06-03T03:09:43+00:00

We know that the classic range random function is like this: public static final

  • 0

We know that the classic range random function is like this:

public static final int random(final int min, final int max) {
    Random rand = new Random();
    return min + rand.nextInt(max - min + 1);  // +1 for including the max
}

I want to create algorithm function for generating number randomly at range between 1..10, but with uneven possibilities like:
1) 1,2,3 -> 3/6 (1/2)
2) 4,5,6,7 -> 1/6
3) 8,9,10 -> 2/6 (1/3)

Above means the function has 1/2 chance to return number between 1 and 3, 1/6 chance to return number between 4 and 7, and 1/3 chance to return number between 8 and 10.

Anyone know the algorithm?

UPDATE:
Actually the range between 1..10 is just served as an example. The function that I want to create would apply for any range of numbers, such as: 1..10000, but the rule is still same: 3/6 for top range (30% portion), 1/6 for middle range (next 40% portion), and 2/6 for bottom range (last 30% portion).

  • 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-03T03:09:44+00:00Added an answer on June 3, 2026 at 3:09 am

    Use the algorithm:

    int temp = random(0,5);
    if (temp <= 2) {
      return random(1,3);
    } else if (temp <= 3) {
     return random(4,7);
    } else  {
     return random(8,10);
    }
    

    This should do the trick.

    EDIT: As requested in your comment:

    int first_lo = 1, first_hi = 3000; // 1/2 chance to choose a number in [first_lo, first_hi]
    int second_lo = 3001, second_hi = 7000; // 1/6 chance to choose a number in [second_lo, second_hi] 
    int third_lo = 7001, third_hi = 10000;// 1/3 chance to choose a number in [third_lo, third_hi] 
    int second
    int temp = random(0,5);
    if (temp <= 2) {
      return random(first_lo,first_hi);
    } else if (temp <= 3) {
     return random(second_lo,second_hi);
    } else  {
     return random(third_lo,third_hi);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know that I can do something like $int = (int)99; //(int) has a
Assuming i have this classic switch , i know that when we are building
The only pre-process directive that I know about in VBScript / Classic ASP is
I know that before_filter is only for controllers in Rails, but I would like
I know that generally speaking, this cant be done, that is get another PC
I know that OpenGL can work with a classic window created by CreateWindow and
How do we model classic many-to-many relationships with CQRS/DDD? I know that both DDD
I know that: ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml"); loads context definition from an XML
I know that I have somewhere inside div on any depth ( maybe one
I know that many of us are writing code to open an InfoWindow when

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.