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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T11:53:37+00:00 2026-06-14T11:53:37+00:00

Let’s say I have a list of ratios in percents. Map<Integer, Integer> ratio =

  • 0

Let’s say I have a list of ratios in percents.

Map<Integer, Integer> ratio = new HashMap<Integer, Integer>();
ratio.put(0, 10);
ratio.put(1, 50);
ratio.put(3, 40);

Then I have a two dimensional array.

int[][] arr = new int[50][50];

Now I want to randomly, but evenly, distribute the defined values 0, 1 and 3 with their respective ratio of 10, 50 and 40 in that array.

How do I do that?

  • 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-14T11:53:38+00:00Added an answer on June 14, 2026 at 11:53 am

    Fill an 1D array with first zeros, second 1 and then 3.

    Something like:

    int arr[N][M] = //;
    int array[N*M] = //;
    int total_array_2D_size = N * M;
    int j = 0;
    Set<Map.Entry<Integer, Integer>> set = ratio.entrySet();  
    
    for (Map.Entry<Integer, Integer> entry : set)
    {
        int k = entry.getKey();        // Take the number
        int ratio = entry.getValue();  // Take the ratio
        int N = (total_array_2D_size * ratio) / 100; // The number of times 'k' should
                                                     // appear on the finally 2D array
    
        for(int i = 0; i < N; i++,j++) array[j] = k; // Fill the 1D array
    }
    

    Than take a Shuffle algorithm and suffle your 1D array. Finally fill up your 2D array.

    for(int i = 0,k = 0; i < N; i++)
      for(int j = 0; j < M; j++,k++)
         arr[i][j] = array[k];
    

    Since your 2D array have 2500 position (50^2), in the finally you should have 250 ‘0’ (0.10 * 50^2), 1250 ‘1’ (0.50 * 50^2) and 1000 ‘3’ (0.4 * 50^2).

    • 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 a sortable list like this: $(.song-list).sortable({ handle : '.pos_handle', axis
Let's say we have a simple function defined in a pseudo language. List<Numbers> SortNumbers(List<Numbers>
let's say I have a select list as follows: <select name='languages'> <option value='german'>German</option> <option
Let's say I have two tables orgs and states orgs is (o_ID, state_abbr) and
Let's say I have a domain object with the following field: private Map<StatType, Double>
Let's say I have two objects, Master and Slave . Slave has a method
Let's say I have the following two lists of tuples myList = [(1, 7),
Let's say I have 2 windows in my application, and two classes responsible for
Let say I have two UIViews: View1: - bounds: 0, 0, 20, 20 -
Let's say I have two assemblies: BusinessLogic and Web. BusinessLogic has an application setting

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.