用java保存随机数

用java保存随机数,java,random,Java,Random,我正在做一个动画处理。我使用的是随机点,我需要为立体视觉执行两次代码。 我的代码中有很多随机变量,所以我应该将它保存在某个地方,以便在第二次运行时使用,或者在运行程序时重新生成相同的“随机”数字字符串。(正如这里所说:) 这种方法可行吗?怎么用?如果我将数字保存在txt文件中,然后读取它,我的程序会运行得较慢吗?最好的方法是什么 谢谢 以下是一个示例: public static void writeRandomDoublesToFile(String filePath, int numbers

我正在做一个动画处理。我使用的是随机点,我需要为立体视觉执行两次代码。 我的代码中有很多随机变量,所以我应该将它保存在某个地方,以便在第二次运行时使用,或者在运行程序时重新生成相同的“随机”数字字符串。(正如这里所说:)

这种方法可行吗?怎么用?如果我将数字保存在txt文件中,然后读取它,我的程序会运行得较慢吗?最好的方法是什么


谢谢

以下是一个示例:

public static void writeRandomDoublesToFile(String filePath, int numbersCount) throws IOException
{
    FileOutputStream fos = new FileOutputStream(new File(filePath));
    BufferedOutputStream bos = new BufferedOutputStream(fos);
    DataOutputStream dos = new DataOutputStream(bos);

    dos.writeInt(numbersCount);
    for(int i = 0; i < numbersCount; i++) dos.writeDouble(Math.random());
}

public static double[] readRandomDoublesFromFile(String filePath) throws IOException
{
    FileInputStream fis = new FileInputStream(new File(filePath));
    BufferedInputStream bis = new BufferedInputStream(fis);
    DataInputStream dis = new DataInputStream(bis);

    int numbersCount = dis.readInt();
    double[] result = new double[numbersCount];

    for(int i = 0; i < numbersCount; i++) result[i] = dis.readDouble();
    return result;
}
public static void writerDomainDoubleStoFile(字符串文件路径,int numbersCount)引发IOException
{
FileOutputStream fos=新FileOutputStream(新文件(文件路径));
BufferedOutputStream bos=新的BufferedOutputStream(fos);
DataOutputStream dos=新的DataOutputStream(bos);
dos.writeInt(numbersCount);
对于(inti=0;i
以下是一个示例:

public static void writeRandomDoublesToFile(String filePath, int numbersCount) throws IOException
{
    FileOutputStream fos = new FileOutputStream(new File(filePath));
    BufferedOutputStream bos = new BufferedOutputStream(fos);
    DataOutputStream dos = new DataOutputStream(bos);

    dos.writeInt(numbersCount);
    for(int i = 0; i < numbersCount; i++) dos.writeDouble(Math.random());
}

public static double[] readRandomDoublesFromFile(String filePath) throws IOException
{
    FileInputStream fis = new FileInputStream(new File(filePath));
    BufferedInputStream bis = new BufferedInputStream(fis);
    DataInputStream dis = new DataInputStream(bis);

    int numbersCount = dis.readInt();
    double[] result = new double[numbersCount];

    for(int i = 0; i < numbersCount; i++) result[i] = dis.readDouble();
    return result;
}
public static void writerDomainDoubleStoFile(字符串文件路径,int numbersCount)引发IOException
{
FileOutputStream fos=新FileOutputStream(新文件(文件路径));
BufferedOutputStream bos=新的BufferedOutputStream(fos);
DataOutputStream dos=新的DataOutputStream(bos);
dos.writeInt(numbersCount);
对于(inti=0;i
如果您只需要在有限的时间内生成相同的序列,那么使用相同的值对随机数生成器进行种子设定以生成相同的序列很可能是最简单、最快的方法。只要确保任何并行线程总是以相同的顺序请求它们的伪随机数,否则您就会遇到麻烦


请注意,如果您更新Java VM,甚至运行修补程序,afaik无法保证相同的序列,因此如果您希望序列具有长时间存储,或者希望能够在Java程序之外使用它,则需要将其保存到文件中。

如果您只需要在有限的时间内生成相同的序列,将相同的值植入随机数生成器以生成相同的序列很可能是最简单和最快的方法。只要确保任何并行线程总是以相同的顺序请求它们的伪随机数,否则您就会遇到麻烦


请注意,如果您更新Java VM,甚至运行修补程序,afaik并不能保证相同的序列,因此如果您希望序列具有长时间存储,或者希望能够在Java程序之外使用它,则需要将其保存到文件中。

好的,有几种方法可以解决此问题。其中之一是将随机变量作为输入保存到文件中,并将该文件名作为参数传递给程序

您可以通过以下两种方法之一来实现,第一种方法是使用args[]参数:

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());
    }
} }
另一种方法是使用扫描仪并从控制台输入读取。它与上面的代码完全相同,但不是
Scanner in=new Scanner(新文件(args[1])和上面的所有验证代码。您可以替换
Scanner in=new Scanner(System.in)
,但这只是为了加载文件

生成这些点的过程可以通过以下方式完成:

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();
    }
}
这两种解决方案都是在Java中读写文件的简单方法。然而,如果您部署了这两种解决方案中的任何一种,您仍然需要对数据进行某种形式的解析

如果是我,我会进行对象序列化,并将已生成的数据结构的二进制副本存储到磁盘,而不必以低效的方式解析和重新分析这些信息。(使用文本文件通常会占用更多磁盘空间。)

下面是您将如何做到这一点(在这里,我将重用已经编写的代码,并在此过程中对其进行评论)

您可以声明一些保存数据的包装类(顺便说一句,您不必总是这样做)

然后,要序列化:

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();
      }
   }
}
然后,要反序列化:

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);
    }
}
问题的另一个替代解决方案(不涉及存储数据)是为提供随机值的任何函数创建一个惰性生成器,并每次提供相同的种子。这样,您根本不必存储任何数据

但是,这仍然比将对象序列化到磁盘并重新加载要慢得多(我认为)。(当然,这是一个非常主观的说法,但我不打算列举那些不真实的案例)。这样做的好处是,它根本不需要任何类型的存储

另一种可能您没有想到的方法是,在生成器函数周围创建一个包装器,用于记忆输出——这意味着以前已经生成的数据将从内存中检索,如果相同的输入为真,则不必再次生成。你可以在这里看到一些关于这方面的参考资料:

记忆你的函数背后的想法