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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T10:40:29+00:00 2026-06-15T10:40:29+00:00

Ok, the example doesn’t matter. I was just using it to put it in

  • 0

Ok, the example doesn’t matter. I was just using it to put it in context. But basically i am wondering how to take a random value from an array/arraylist?

I tried using this: int random = (int)input[Math.floor(Math.random() * input.length)];
but it highlights potential loss of precision even though i’ve already cast it.
Have i cast it wrong?

The example is irrelevant really and i do accept answers to all my questions.
Initially i wanted to see a correct answer without influence from me to see if i was on the right track.

  • 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-15T10:40:30+00:00Added an answer on June 15, 2026 at 10:40 am

    This is exactly the bin packing problem. It is also NP-Complete, and thus there is no known polynomial solution (and the general assumption is one does not exist, but it is not proven yet).

    The problem is widely studied if you are looking for approximation algorithm, or a relatively efficient exact algorithm


    PS – the random approach you suggest will get you a solution, but it won’t be OPTIMAL.

    Counter example:

    arr = [2,3,4,1] , size=5
    

    A possible solution to your algorithm could be: select 3, select 1, select 4, select 2, which will yield:

    [3,1],[4],[2]
    

    While the optimal solution will be [3,2],[4,1]

    (-) I believe a greedy approach (chose the highest, put it in an available bin – if it fits, and if it doesn’t – “open” a new bin) will do better then the randomized solution but will still be not optimal, with the counter example:

    arr = [5,4,4,3,2,2] size = 10
    

    greedy will yield [5,4],[4,3,2],[2] while optimal is [5,3,2],[4,4,2]


    Regarding the loss pf precision – it happens because Math.floor(double) yields a long, while you use it as an int (and you might theoretically lose data when doing so). However, it is not a problem in your case since Math.floor(Math.random() * input.length is guaranteed to be in the range of an int because input.length is an int and Math.random() < 1.

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

Sidebar

Related Questions

The following jQuery example should put some text into the div, but it doesn't.
I found an example in MSDN for string to datetime conversion. But it doesn't
I'm currently learning Sencha Touch from a book. Following the example doesn't work for
I know that the foreach loop used in the following example doesn't compile. But
I'm trying to use the AbortableTask feature of Celery but the documentation example doesn't
I'm learning C++ from a book and the following example doesn't work in codeblocks.
I'm trying to use Live Search 2.0 but even a simple example doesn't seem
[Update]: my initial example doesn't reflect my problem. Updated the sample, hopfully it is
Ideally I want a function something like the following (this example doesn't compile): void
Why doesn't \w match Unicode word characters (for example, ğ,İ,ş,ç,ö,ü) in a Perl regular

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.