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

  • Home
  • SEARCH
  • 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 4026048
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T10:55:19+00:00 2026-05-20T10:55:19+00:00

If we have a Map<T, Integer> , let’s say the Integer value represents how

  • 0

If we have a Map<T, Integer>, let’s say the Integer value represents “how many” Ts there are. Thus, I want to uniformly select a T based on its Integer value. If the map contains Strings with “a”=4 and “b”=6, then I want it so that 40% of the time “a” is selected and 60% of the time “b” is selected.

Most importantly, I’d like this in O(n), n being two (not ten) in my previous example. I originally made an ArrayList containing the keys by how many values it had (and simply returning any random index), but this process is not only very slow, but completely counterintuitive for what the Map<T, Integer> represents.

  • 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-20T10:55:19+00:00Added an answer on May 20, 2026 at 10:55 am

    OP here.

    I came up with an elegant solution! For any misunderstandings: My original idea of storing all the keys by how many values in an ArrayList was completely disregarding the point of using a Map to store “instances of the Key using Integers”; any similar solutions are counterproductive! Assuming the Map is unordered, here is my solution:

    public T randomPick(Random r) {
    
            int randomValue = r.nextInt(size());
            int currentSum = 0;
            T lastElement = null;
    
            for (T t : map.keySet()){
                if (randomValue < currentSum + map.get(t)){
                    return t;
                }
                currentSum+= map.get(t);
                lastElement = t;
            }
            return lastElement;
        }
    

    It compares the random value with a current sum + the current element's value. If it is less than that, we return the current key. Else, keep going and add that value to the sum. If it is the case such that the random value is never less than any of the values, we return the lastElement.

    Hope this clears it up.

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

Sidebar

Related Questions

Let's say I have two same strings inside an ArrayList... is there a way
If I have a Map like this: HashMap<Integer, ComparableObject> map; and I want to
I have unsorted map of key value pairs. input = { xa => xavalue,
I have a Map which is to be modified by several threads concurrently. There
I want to have a map that has a homogeneous key type but heterogeneous
Is there a way in Java to have a map where the type parameter
Let's say I have a LazySeq of java.lang.Character like (\b \ \! \/ \b
Let's say I have a bean like below. class Customer{ private String code; private
Let's say I have base class FooParent , and it has a numerous amount
let's say we have a CountryList object in our application that should return 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.