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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T19:33:00+00:00 2026-05-23T19:33:00+00:00

In Java, given n Items, each with weight w , how does one choose

  • 0

In Java, given n Items, each with weight w, how does one choose a random Item from the collection with a chance equal to w?

Assume each weight is a double from 0.0 to 1.0, and that the weights in the collection sum to 1. Item.getWeight() returns the Item’s weight.

  • 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-23T19:33:00+00:00Added an answer on May 23, 2026 at 7:33 pm

    2020 Update (interesting how this got 37 upvotes with a glaring bug in the 2011 version below):

    • Fix the impossibility to select the last item when Math.random() yields a number very close to 1.0, and we are unlucky with floating point precision: random index -1 would be the result, which is obviously wrong.
    • Some code compaction
    • Less variable names used
    Item[] items = ...;
    
    // Compute the total weight of all items together.
    // This can be skipped of course if sum is already 1.
    double totalWeight = 0.0;
    for (Item i : items) {
        totalWeight += i.getWeight();
    }
    
    // Now choose a random item.
    int idx = 0;
    for (double r = Math.random() * totalWeight; idx < items.length - 1; ++idx) {
        r -= items[idx].getWeight();
        if (r <= 0.0) break;
    }
    Item myRandomItem = items[idx];
    

    2011 version (for comparison left in):

    Item[] items = ...;
    
    // Compute the total weight of all items together
    double totalWeight = 0.0d;
    for (Item i : items)
    {
        totalWeight += i.getWeight();
    }
    // Now choose a random item
    int randomIndex = -1;
    double random = Math.random() * totalWeight;
    for (int i = 0; i < items.length; ++i)
    {
        random -= items[i].getWeight();
        if (random <= 0.0d)
        {
            randomIndex = i;
            break;
        }
    }
    Item myRandomItem = items[randomIndex];
    

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

Sidebar

Related Questions

Given a collection of integers, what's a Java algorithm that will give all pairs
Given a Collection of Strings, how would you join them in plain Java, without
Given 2 java AffineTransform items, how can I interpolate between them. I need the
Given Java's write once, run anywhere paradigm and the fact that the Java tutorials
I am looking for a framework to turn given Java class into WebService (may
I want to measure API evolution for a given Java project, in particular new/renamed
Given a java.util.Date object how do I go about finding what Quarter it's in?
Given two java classes, A and B, where A is usually instantiated via B,
Given the Java code below, what's the closest you could represent these two static
If I'm given two Java Libraries in Jar format, 1 having no bells and

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.