Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Well, there's a couple of ways that you can approach this problem. One of them would be to save the random variables as input into a file and pass that file name as a parameter to your program. </p> <p>And you could do that in one of two ways, the first of which would be to use the args[] parameter:</p> <pre><code>import java.io.*; import java.util.*; public class bla { public static void main(String[] args) { // You'd need to put some verification code here to make // sure that input was actually sent to the program. Scanner in = new Scanner(new File(args[1])); while(in.hasNextLine()) { System.out.println(in.nextLine()); } } } </code></pre> <p>Another way would be to use Scanner and read from the console input. It's all the same code as above, but instead of <code>Scanner in = new Scanner(new File(args[1]));</code> and all the verification code above that. You'd substitute <code>Scanner in = new Scanner(System.in)</code>, but that's just to load the file. </p> <p>The process of generating those points could be done in the following manner:</p> <pre><code>import java.util.*; import java.io.*; public class generator { public static void main(String[] args) { // You'd get some user input (or not) here // that would ask for the file to save to, // and that can be done by either using the // scanner class like the input example above, // or by using args, but in this case we'll // just say: String fileName = "somefile.txt"; FileWriter fstream = new FileWriter(fileName); BufferedWriter out = new BufferedWriter(fstream); out.write("Stuff"); out.close(); } } </code></pre> <p>Both of those solutions are simple ways to read and write to and from a file in Java. However, if you deploy either of those solutions, you're still left with some kind of parsing of the data. </p> <p>If it were me, I'd go for object serialization, and store a binary copy of the data structure I've already generated to disk rather than having to parse and reparse that information in an inefficient way. (Using text files, usually, takes up more disk space.)</p> <p>And here's how you would do that (Here, I'm going to reuse code that has already been written, and comment on it along the way) <a href="http://www.tutorialspoint.com/java/java_serialization.htm" rel="nofollow">Source</a></p> <p>You declare some wrapper class that holds data (you don't always have to do this, by the way.)</p> <pre><code> public class Employee implements java.io.Serializable { public String name; public String address; public int transient SSN; public int number; public void mailCheck() { System.out.println("Mailing a check to " + name + " " + address); } } </code></pre> <p>And then, to serialize:</p> <pre><code>import java.io.*; public class SerializeDemo { public static void main(String [] args) { Employee e = new Employee(); e.name = "Reyan Ali"; e.address = "Phokka Kuan, Ambehta Peer"; e.SSN = 11122333; e.number = 101; try { FileOutputStream fileOut = new FileOutputStream("employee.ser"); ObjectOutputStream out = new ObjectOutputStream(fileOut); out.writeObject(e); out.close(); fileOut.close(); }catch(IOException i) { i.printStackTrace(); } } } </code></pre> <p>And then, to deserialize: </p> <pre><code>import java.io.*; public class DeserializeDemo { public static void main(String [] args) { Employee e = null; try { FileInputStream fileIn = new FileInputStream("employee.ser"); ObjectInputStream in = new ObjectInputStream(fileIn); e = (Employee) in.readObject(); in.close(); fileIn.close(); }catch(IOException i) { i.printStackTrace(); return; }catch(ClassNotFoundException c) { System.out.println(.Employee class not found.); c.printStackTrace(); return; } System.out.println("Deserialized Employee..."); System.out.println("Name: " + e.name); System.out.println("Address: " + e.address); System.out.println("SSN: " + e.SSN); System.out.println("Number: " + e.number); } } </code></pre> <p>Another alternative solution to your problem, that does not involve storing data, is to create a lazy generator for whatever function that provides you your random values, and provide the same seed each and every time. That way, you don't have to store any data at all. </p> <p>However, that still is quite a bit slower (I think) than serializing the object to disk and loading it back up again. (Of course, that's a really subjective statement, but I'm not going to enumerate cases where that is not true). The advantage of doing that is so that it doesn't require any kind of storage at all. </p> <p>Another way, that you may have not possibly thought of, is to create a wrapper around your generator function that memoizes the output -- meaning that data that has already been generated before will be retrieved from memory and will not have to be generated again if the same inputs are true. You can see some resources on that here: <a href="http://onjava.com/pub/a/onjava/2003/08/20/memoization.html" rel="nofollow">Memoization source</a></p> <p>The idea behind memoizing your function calls is that you save time without persisting to disk. This is ideal if the same values are generated over and over and over again. Of course, for a set of random points, this isn't going to work very well if every point is unique, but keep that in the back of your mind. </p> <p>The really interesting part comes when considering the ways that all the previous strategies I've described in this post can be combined together.</p> <p>It'd be interesting to setup a Memoizer class, like described in the second page of <a href="http://onjava.com/pub/a/onjava/2003/08/20/memoization.html" rel="nofollow">2</a> and then implement java.io.Serialization in that class. After that, you can add methods <code>save(String fileName)</code> and <code>load(String fileName)</code> in the memoizer class that make serialization and deserialization easier, so you can persist the cache used to memoize the function. Very useful. </p> <p>Anyway, enough is enough. In short, just use the same seed value, and generate the same point pairs on the fly. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload