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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T06:21:32+00:00 2026-05-31T06:21:32+00:00

I am writing a multi-threaded Java program that generates lot of random numbers. Additional

  • 0

I am writing a multi-threaded Java program that generates lot of random numbers.

Additional Details:
These numbers are used to create a list of random numbers from 0-99 without repetition and such that every number in the range 0-99 exists in the list (In other words, the list contains 100 unique elements in the range 0-99).

Generating Random Numbers [Things already tried!]

  1. I have an ArrayList of numbers from 0-100. I generate a random number and use it as an index which is used to pop out an element from the ArrayList.
  2. I have used Collections.shuffle().

Here is the code for approach 1:

ArrayList<Integer> arr = new ArrayList<Integer>(); 
for (int i = 0; i < N; i++){
 arr.add(i, i);
}

for(int i=0; i<N; i++){
  int indx = rand.nextInt(arr.size());
  res.add(arr.get(indx));
  arr.remove(indx);
}

For second approach, I replaced the second for loop with Collections.shuffle(arr).

As generating list of random numbers is the most expensive part of my algorithm, I want to optimize it. This brings me to the questions:

  1. What is the fastest way to generate random numbers?
  2. What is the fastest way to generate the list of random numbers as described above?

PS:

  1. I found Collections.shuffle() to be slower than the first approach
  2. Someone suggested me using rngd to generate random numbers from hardware in Unix. Has anyone tried this before? How do you 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-05-31T06:21:34+00:00Added an answer on May 31, 2026 at 6:21 am

    I think the problem with Collections.shuffle() is that is uses default Random instance which is a thread-safe singleton. You say that your program is multi-threaded, so I can imagine synchronization in Random being a bottle-neck.

    If you are happily running on Java 7, simply use ThreadLocalRandom. Look carefully, there is a version of shuffle() taking Random instance explicitly:

    Collections.shuffle(arr, threadLocalRandom);
    

    where threadLocalRandom is created only once.

    On Java 6 you can simply create a single instance of Random once per thread. Note that you shouldn’t create a new instance of Random per run, unless you can provide random seed every time.

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

Sidebar

Related Questions

I'm writing a multi-threaded program that needs to be able to check if a
I'm writing a multi-threaded Java web crawler. From what I understand of the web,
I've read a lot recently about how writing multi-threaded apps is a huge pain
I am on MacOSX. I am writing a multi threaded program. One thread does
I am writing a multi-threaded program using OpenMP in C++. At one point my
I am writing a multi-threaded application in Java in order to improve performance over
I'm writing a multi-threaded server application in Java. Everything works fine , but there's
I'am writing a multi-threaded program in C. Before creating the threads, a global python
For example, I'm writing a multi-threaded time-critical application that processes and streams audio in
I am writing a multi-threaded application that mimics a movie theater. Each person involved

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.