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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T04:40:50+00:00 2026-06-07T04:40:50+00:00

I am using this to try to create a weighted random: int choice_weight[] =

  • 0

I am using this to try to create a weighted random:

int choice_weight[] = { 25, 25, 25, 25 };
int num_choices = choice_weight.length;
Random random = new Random();
int i;
int sum_of_weight = 0;

for (i = 0; i < num_choices; i++) {
    sum_of_weight += choice_weight[i];
}

int rnd = random.nextInt(sum_of_weight);
System.out.println(rnd);

for (i = 0; i < num_choices; i++) {
    if (rnd < choice_weight[i])
        rnd -= choice_weight[i];
}

I am using a swtich statement to test and see if the cases are:

case 1:

case 2:

case 3:

case 4:

The problem is that random.nextInt(sum_of_weight) is generating numbers between 100 and 0 which is based on my 4 weights of 25,25,25,25. I need a number to match one of the possible cases 1-4? Or should I change up my method of testing?

This whole process is kind of confusing me, some help would be greatly appreciated.

So basically it will be a 25 percent chance a particular number will be chosen. Also, I will be changing the percentages throughout the program’s life cycle.

  • 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-07T04:40:51+00:00Added an answer on June 7, 2026 at 4:40 am

    Shouldn’t you “increase” your range when testing for your number? Ie:

    System.out.println(rnd);
    
    int winner=0;
    int rangeHigh=0;
    for (i = 0; i < num_choices; i++) {
       rangeHigh += choice_weight[i];
       if (rnd < rangeHigh){
          winner = (i+1);
          break;
       }
    }
    System.out.println("lucky winner number: " + winner);
    

    Basically you’re generating a number between 0-99 and checking if you values are:

    1. < 25  //bucket 1 - [0-24]
    2. < 50  //bucket 2 - [25-49]
    3. < 75  //bucket 3 - [50-74]
    4. < 100  //bucket 4 - [75-99]
    

    On second look the way you have it will work too (w/ adjustments):

    int winner = 0;
    for (i = 0; i < num_choices; i++) {
        if (rnd < choice_weight[i]){
           winner = i+1;
           break;
        }
        rnd -= choice_weight[i];
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i want to create new file iam using this code try { File file
I attempting to create custom tabs using this . But when I try to
I'm using this code and I'm beyond frustration: try { $dbh = new PDO('mysql:dbname='
i am using the 960 grid system to try and create a layout... I
I am try to create extension of principal for organizationalUnit using this code below
Say I create a table People using this: rails new app; cd app; rails
I'm using this to (try) to validate a 'strong' password in ColdFusion 7. if
I am using this function try to make a request to the server. I
I am using this tutorial to try to replace the default TitleBar with a
I'm using this piece of code to try and save an image to the

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.