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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T14:14:24+00:00 2026-06-18T14:14:24+00:00

I have created a method to randomize the list: public <T> List<T> randomize(List<T> list)

  • 0

I have created a method to randomize the list:

public <T> List<T> randomize(List<T> list) {
    LinkedList<T> randomizedList = new LinkedList<>();
    Random random = new Random(list.size());
    for (int i = random.nextInt(); i < list.size(); i++) {
        randomizedList.add(list.get(i));
    }
    return randomizedList;
}

The list that I am passing to this method contains for e.g. five elements. When I am creating Random random = new Random(list.size()); I expect so when I call random.nextInt() what it will return me the random integer which will be an index of the list element.

But when I call random.nextInt(); instead of returning the number from the interval [0, 4] (which I expect to be returned) it returns me the value of for e.g.: -349120689. Which gives me an java.lang.IndexOutOfBoundsException: Index: -349120689, Size: 5.

Why is this happening and how to solve this?

  • 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-18T14:14:25+00:00Added an answer on June 18, 2026 at 2:14 pm
    1. new Random(list.size()); this sets the seed of the Random number generator to list.size(); I suggest changing to new Random() (which will give you a seed based on the current time of the system). Preferably though, you’d want to reuse the same Random object at all times.

    2. random.nextInt(); here is where you’d want to put random.nextInt(list.size()); which will give you a number from 0 to list.size() – 1.

    3. Even with the above changes, your code would just give you a sublist of the list starting at a random index and going until the end. Use Collections.shuffle(list) instead.

    To do a real shuffling, you would need to “remember” which elements
    you have inserted or not. In pseudo-code, you could do the
    following:

    • Copy the original list to a new one, let’s call it “orig”
    • Create a new, empty, list, let’s call it “result”
    • As long as the orig list has elements, get an element at random index and add it to the result list. Remove the chosen element from orig list.
    • Return result list.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created a new method in one of the project's controllers that it
I have created a gem that adds a new method to the class ActiveRecord::Base.
I have created a Webpage which will post as post method..not as get method.
I have created a method for reading inbox new messages by using exchange server
I have created a method which takes generate random numbers with the condition that
I have created this method which is an object factory: public static T GetService<T>(T
I have created a Method shown below, public BOEod CheckCommandStatus(BOEod pBo, IList<string> pProperties) {
I have created a method public boolean addWord(Word w, String name) { try {
I have created a method to check if the keyboard is used in an
I have created a method that should ideally take a single Account object 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.