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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T19:49:38+00:00 2026-06-09T19:49:38+00:00

I want a way to generate a file containing random bits in Java. What

  • 0

I want a way to generate a file containing random bits in Java. What will create the random file the fastest? I want to create files of any specified size containing random bits. I want to be able to generate a 2GB file in a matter of minutes (less than a minute if possible). The technique I’m using right now takes hours to do 2GB:

...
private static Random r = new Random();

private static int rand(int lo, int hi) {
    int n = hi - lo + 1;
    int i = r.nextInt() % n;
    if (i < 0) {
        i = -i;
    }
    return lo + i;
}
...
fos = new FileOutputStream(hdFiller);
for(long i = 0; i < maxFileSize; i++) {
    int idx = rand(0,32);
    fos.write(idx);
}
fos.close();
...

There has got to be a way to do this faster, maybe even in less than a minute for 2GB.

Thanks.

  • 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-09T19:49:40+00:00Added an answer on June 9, 2026 at 7:49 pm

    If you want to generate random bits all at once, rather than looping, take a look at the java.util.Random method nextBytes(byte[]) which fills the specified byte array with random bytes. Create a byte array exactly large enough for 2GiB of data and you can generate the whole random bit source in one go.

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

Sidebar

Related Questions

Is there any way in Perl to generate file handles programmatically? I want to
I want to generate custom methods in Eclipse like the way we can generate
I won't want to have edit any working sets. I just want a way
I want a robust way to upload a file. That means that I want
Using C++, I want to generate a file in which I have to add
I'm using C# and ASP.NET 2.5. I want a simple way to generate a
Is there a way to generate an HTML file using a PHP script which
When I compile my project in Netbeans, I want to automatically generate a file
I want the compiler to run preprocessing, generate all the .i files like it
Is there a way to generate a migration file or db schema file from

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.