Reputation: 1125
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
Upvotes: 1
Views: 401
Reputation: 533570
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
Upvotes: 4
Reputation: 15389
The fastest way is to run the program once, note down the random numbers generated, and then hardcode the random numbers in an array in your program! Then, the next time onwards, your program can read these same values from the array.
So suppose you program generates random numbers as follows -
0.34, 0.15, 0.28, 0.45, ...
You can then define an array and store these values in it.
randomValues[0] = 0.34;
randomValues[1] = 0.15;
randomValues[2] = 0.28;
randomValues[3] = 0.45;
.
.
.
Then each time, simply use an index to get the random number you want.
index = 0;
randomNumber = randomValues[index];
index++; // so the next time, you can get the next random number in sequence.
Upvotes: 1
Reputation: 1603
Generate them once, then save them into a file... afterthat, everytime you want to run your program, you have to load these values again.
Upvotes: 0
Reputation: 195129
a couple of ways you may consider to go
1 using the same seed to generate random number
2 generate the random and save them in a file. your program two reads the file to get values.
Upvotes: 3