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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T12:12:28+00:00 2026-05-31T12:12:28+00:00

Using java I want to generate some random values in one program and then

  • 0

Using java I want to generate some random values in one program and then use these values in some other program everytime the 2nd program is executed.

Purpose of this is to generate random values once and then hold and keep them constant for every run of the program later. Is it possible in some way? 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-05-31T12:12:29+00:00Added an answer on May 31, 2026 at 12:12 pm

    When you exit a program, anything you don’t store in a file is lost.

    I suspect you don’t need to worry about IO as much as you think. You should be able to read millions of values in a few milli-seconds. In fact you should be able to generate millions of random numbers in a fraction of a second.

    Random random = new Random(1);
    long start = System.nanoTime();
    int values = 1000000;
    for (int i = 0; i < values; i++)
        random.nextInt();
    long time = System.nanoTime() - start;
    System.out.printf("Took %.3f seconds to generate %,d values%n",
                      time / 1e9, values);
    

    prints

    Took 0.015 seconds to generate 1,000,000 values
    

    Generating and writing

    int values = 1000000;
    ByteBuffer buffer = ByteBuffer.allocateDirect(4 * values).order(ByteOrder.nativeOrder());
    
    Random random = new Random(1);
    long start = System.nanoTime();
    for (int i = 0; i < values; i++)
        buffer.putInt(random.nextInt());
    buffer.flip();
    FileOutputStream fos = new FileOutputStream("/tmp/random.ints");
    fos.getChannel().write(buffer);
    fos.close();
    long time = System.nanoTime() - start;
    System.out.printf("Took %.3f seconds to generate&write %,d values%n", time / 1e9, values);
    

    prints

    Took 0.021 seconds to generate&write 1,000,000 values
    

    Reading the same file.

    long start2 = System.nanoTime();
    FileInputStream fis = new FileInputStream("/tmp/random.ints");
    MappedByteBuffer buffer2 = fis.getChannel().map(FileChannel.MapMode.READ_ONLY, 0, values * 4).order(ByteOrder.nativeOrder());
    for (int i = 0; i < values; i++)
        buffer2.getInt();
    fis.close();
    long time2 = System.nanoTime() - start2;
    System.out.printf("Took %.3f seconds to read %,d values%n", time2 / 1e9, values);
    

    prints

    Took 0.011 seconds to read 1,000,000 values
    

    Reading the same file repeatedly

    long sum = 0;
    int repeats = 1000;
    for (int j = 0; j < repeats; j++) {
        buffer2.position(0);
        for (int i = 0; i < values; i++)
            sum += buffer2.getInt();
    }
    fis.close();
    long time2 = System.nanoTime() - start2;
    System.out.printf("Took %.3f seconds to read %,d values%n", time2 / 1e9, repeats * values);
    

    prints

    Took 1.833 seconds to read 1,000,000,000 values
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using Java. I want to have a setter method of one class that
I want to publish message on Facebook's fan page using JAVA/GWT API. Any one
We're using some C library in our Java project. Several years ago some other
I want to get some random text generated. I tried writing a basic Java
I want to generate java code from xsd using JAXB 2.1 XJC. I have
I want to generate array position from 1 to 1000 randomly using some mathematical
Using Java (1.6) I want to split an input string that has components of
I want city name from IP address using Java is there any idea to
I want to take a snapshot with my webcam using java and save it
I am using java to send mail. I want to set the from mail

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.