如何将从时间(NULL)生成的随机种子存储在文本文件中?

时间:2022-11-29 20:51:20

I am creating random numbers for my code and I am using the seeding method for the purpose of debugging. What I want to do is to store the return value of time(NULL) function which will be used by the srand() function. The value is to be stored in a log text file so that it can be used to analyze problems that occur in the code.

我正在为我的代码创建随机数,我正在使用种子方法进行调试。我想要做的是存储将由srand()函数使用的time(NULL)函数的返回值。该值将存储在日志文本文件中,以便可用于分析代码中出现的问题。

So how should I printf the exact value used as the seed in the srand function?

那么我应该如何printf在srand函数中用作种子的确切值?

2 个解决方案

#1


4  

As already commented, time_t can be virtually anything, the description even changed with versions of the standard, so the only thing you know is that it is a number. I'd say the only portable way to write it to a file is to read the bytes of its representation and store them (*), e.g. in hex format, or directly as binary using fwrite(). For hex format, you could alias it with a char * and read the bytes by indexing from 0 to sizeof(time_t) - 1.

正如已经评论过的那样,time_t几乎可以是任何东西,描述甚至随着标准的版本而改变,所以你唯一知道的是它是一个数字。我要说将它写入文件的唯一可移植方法是读取其表示的字节并存储它们(*),例如,十六进制格式,或使用fwrite()直接作为二进制。对于十六进制格式,您可以使用char *对其进行别名,并通过从0到sizeof(time_t) - 1的索引来读取字节。

But then, you don't need the exact return value of time() for what you want to achieve, you only need the seed that was actually used! If you look at the srand() function, it takes an unsigned int. So just convert whatever time(NULL) returns to an unsigned int first and store that.

但是,你不需要time()的确切返回值来实现你想要实现的目标,你只需要实际使用的种子!如果你看一下srand()函数,它需要一个unsigned int。所以只需转换任何时间(NULL)首先返回unsigned int并存储它。

Note that the inner workings of the PRNG aren't specified in the standard either, so if you obtain a logfile from a user, there's no guarantee you can reproduce the same sequence of random numbers from it if he linked against a different C library.

请注意,PRNG的内部工作方式也未在标准中指定,因此如果您从用户获取日志文件,则无法保证如果他链接到不同的C库,则可以从中重现相同的随机数序列。


(*) This of course is not portable at runtime between different implementations, as they will have different representations. I guess the question isn't about that, but for completeness, if you really want to store a time value, the way to go would be to store e.g. the components of a struct tm or use something like strftime().

(*)当然,这在不同的实现之间在运行时是不可移植的,因为它们将具有不同的表示。我想这个问题与此无关,但为了完整起见,如果你真的想存储一个时间价值,那么可以选择存储例如struct tm的组件或使用strftime()之类的东西。

#2


1  

The function time() returns time_t type which can be any integer or real-floating type.

函数time()返回time_t类型,可以是任何整数或实数浮点类型。

But you can cast it to a known type:

但您可以将其转换为已知类型:

printf("%lld\n", (long long) time(NULL));

If you just want to store the binary time_t value in order to read it back later you could do:

如果您只想存储二进制time_t值以便稍后读取它,您可以执行以下操作:

Write:

time_t t = time(NULL);
/* ... open file handle */
fwrite(&t, sizeof(t), 1, file);

Read:

time_t t = 0;
/* ... open file handle */
fread(&t, sizeof(t), 1, file);

When doing this you have to make sure that wrting and reading is done by the same binary (or different binaries but same compiler).

执行此操作时,您必须确保wrting和读取是由相同的二进制文件(或不同的二进制文件,但相同的编译器)完成的。

#1


4  

As already commented, time_t can be virtually anything, the description even changed with versions of the standard, so the only thing you know is that it is a number. I'd say the only portable way to write it to a file is to read the bytes of its representation and store them (*), e.g. in hex format, or directly as binary using fwrite(). For hex format, you could alias it with a char * and read the bytes by indexing from 0 to sizeof(time_t) - 1.

正如已经评论过的那样,time_t几乎可以是任何东西,描述甚至随着标准的版本而改变,所以你唯一知道的是它是一个数字。我要说将它写入文件的唯一可移植方法是读取其表示的字节并存储它们(*),例如,十六进制格式,或使用fwrite()直接作为二进制。对于十六进制格式,您可以使用char *对其进行别名,并通过从0到sizeof(time_t) - 1的索引来读取字节。

But then, you don't need the exact return value of time() for what you want to achieve, you only need the seed that was actually used! If you look at the srand() function, it takes an unsigned int. So just convert whatever time(NULL) returns to an unsigned int first and store that.

但是,你不需要time()的确切返回值来实现你想要实现的目标,你只需要实际使用的种子!如果你看一下srand()函数,它需要一个unsigned int。所以只需转换任何时间(NULL)首先返回unsigned int并存储它。

Note that the inner workings of the PRNG aren't specified in the standard either, so if you obtain a logfile from a user, there's no guarantee you can reproduce the same sequence of random numbers from it if he linked against a different C library.

请注意,PRNG的内部工作方式也未在标准中指定,因此如果您从用户获取日志文件,则无法保证如果他链接到不同的C库,则可以从中重现相同的随机数序列。


(*) This of course is not portable at runtime between different implementations, as they will have different representations. I guess the question isn't about that, but for completeness, if you really want to store a time value, the way to go would be to store e.g. the components of a struct tm or use something like strftime().

(*)当然,这在不同的实现之间在运行时是不可移植的,因为它们将具有不同的表示。我想这个问题与此无关,但为了完整起见,如果你真的想存储一个时间价值,那么可以选择存储例如struct tm的组件或使用strftime()之类的东西。

#2


1  

The function time() returns time_t type which can be any integer or real-floating type.

函数time()返回time_t类型,可以是任何整数或实数浮点类型。

But you can cast it to a known type:

但您可以将其转换为已知类型:

printf("%lld\n", (long long) time(NULL));

If you just want to store the binary time_t value in order to read it back later you could do:

如果您只想存储二进制time_t值以便稍后读取它,您可以执行以下操作:

Write:

time_t t = time(NULL);
/* ... open file handle */
fwrite(&t, sizeof(t), 1, file);

Read:

time_t t = 0;
/* ... open file handle */
fread(&t, sizeof(t), 1, file);

When doing this you have to make sure that wrting and reading is done by the same binary (or different binaries but same compiler).

执行此操作时,您必须确保wrting和读取是由相同的二进制文件(或不同的二进制文件,但相同的编译器)完成的。