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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T04:46:03+00:00 2026-06-16T04:46:03+00:00

I have the following method: public LinkedList<Object> init(int counter) { LinkedList<Object> list = new

  • 0

I have the following method:

public LinkedList<Object> init(int counter) {
    LinkedList<Object> list = new LinkedList<Object>();
    double decision, value, key;
    int max1 = 700;
    int max2 = 1000;

    for (int i = 0; i < counter; i++) {
        decision= Math.random();

        if (decision<= 0.2) {
            key = Math.random() * 1.5;
            value= Math.random() * max1 ;
            list.add(new A(value, key));
        } else {
            value= Math.random() * max2 ;
            list.add(new B(value));
        }
     }

     return list;
}

The problem I get is this: if I call this method using

init(100);

and then check the size of the resulting list, it is not always 100. Instead, the number of elements in the list varies depending on the values i choose for max1 and max2. If I choose max2 = 1000000 for example, I end up with a list of about 15 elements. I suspect this has something to do with how Math.random() works, but have no idea how it happens. Is the problem about threading?

In case someone wants to try this, here are templates for classes A and B (the workings of which don’t play into this):

public class A {
    public A(double value, double key) {}
} 

public class B {
    public B(double value) {}
}
  • 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-16T04:46:05+00:00Added an answer on June 16, 2026 at 4:46 am

    I suspected Math.random() might be running a thread in the background; I did not create any additional threads myself.

    No, Math.random() does not run any threads in the background.

    In fact, your function is perfectly fine. I’ve tested it extensively and it does exactly what one would expect. It always returns exactly counter elements.

    I therefore have to conclude that your problem lies elsewhere, i.e. outside the code that you’re showing us.

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

Sidebar

Related Questions

I have the following method and interface: public object ProcessRules(List<IRule> rules) { foreach(IRule rule
public void wahey(List<Object> list) {} wahey(new LinkedList<Number>()); The call to the method will not
I have the following method: public IEnumerable<object> GetParameters(Context context) { yield return new[] {
I have the following method: public List<Alert> GetMonthlyAlertsByAccountID(Int32 AccountID, params int[] alertTypes) { List<Alert>
I have the following method public static void SerializeToXMLFile(Object obj,Type type, string fileName) {
I have the following method: public object[] GetEventsByUser(DateTime start, DateTime end, string fullUrl) The
I have the following method : public List<string> someMethod() { // populate list of
I have the following method: public IEnumerable<Foo> GetFoo(int x, string y) { return from
I have the following method: public DataTable.DataTablesPage<DataTable.UserModel> DataTableUserListByAttendees(int geoArea, int CurrentUserID) { var result
Given the following method: public static void SetPropertyValue(object target, string propName, object value) {

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.