C 如何每次都使用相同的随机数组?
我将随机数组保存在文本文件中,以便再次加载它。有没有更好的方法来保存它而不使用txt文件C 如何每次都使用相同的随机数组?,c,arrays,C,Arrays,我将随机数组保存在文本文件中,以便再次加载它。有没有更好的方法来保存它而不使用txt文件 for(i=0;i<10000;i++); clientdata[i]=rand(); FILE * fp; fp = fopen ("client.txt", "w+"); fwrite(clientdata, sizeof clientdata[0], sizeof clientdata / sizeof clientdata[0], f); fclose(f); for(i=0;i来自C库
for(i=0;i<10000;i++);
clientdata[i]=rand();
FILE * fp;
fp = fopen ("client.txt", "w+");
fwrite(clientdata, sizeof clientdata[0], sizeof clientdata / sizeof clientdata[0], f);
fclose(f);
for(i=0;i来自C库(rand48?)的函数rand
)生成一个随机序列
然而,这是预先定义的
可以使用srand
选择不同的序列。要获得相同的序列,请对srand
使用相同的值
来自C库(rand48?)的函数rand
生成一个随机序列
然而,这是预先定义的
可以使用srand
选择不同的序列。要获得相同的序列,请对srand
使用相同的值
您可以像这样使用随机种子,以确保每次生成相同的随机数:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int seed = 1892; //any number, this is the random seed
srand(seed); //call this with the random seed argument
for(i=0;i<10000;i++);
clientdata[i]=rand();
//do something, you will generate the same random data every time
}
#包括
#包括
int main()
{
int seed=1892;//任何数字,这是随机种子
srand(seed);//使用随机seed参数调用此函数
对于(i=0;i您可以像这样使用随机种子,以确保每次生成相同的随机数:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int seed = 1892; //any number, this is the random seed
srand(seed); //call this with the random seed argument
for(i=0;i<10000;i++);
clientdata[i]=rand();
//do something, you will generate the same random data every time
}
#包括
#包括
int main()
{
int seed=1892;//任何数字,这是随机种子
srand(seed);//使用随机seed参数调用此函数
对于(i=0;i使用srand
启动随机数序列,并重用相同的种子值以重新生成相同的序列:
#include <stdio.h>
#include <stdlib.h>
int main ()
{
for(int i=1; i<=2; i++){
srand (22); // can be any value -- just the same to restart the series
for(int j=1; j<=10; j++)
printf ("Random Number %d: %d\n", j, rand() %100);
}
return 0;
}
// regenerates the same series of rand numbers...
#包括
#包括
int main()
{
对于(int i=1;i使用srand
启动随机数序列,并重用相同的种子值以重新生成相同的序列:
#include <stdio.h>
#include <stdlib.h>
int main ()
{
for(int i=1; i<=2; i++){
srand (22); // can be any value -- just the same to restart the series
for(int j=1; j<=10; j++)
printf ("Random Number %d: %d\n", j, rand() %100);
}
return 0;
}
// regenerates the same series of rand numbers...
#包括
#包括
int main()
{
对于(int i=1;i您需要指定随机种子指定随机种子是什么意思?您调用文件client.txt
,这意味着它是一个文本文件,但不编写“文本”但是clientdata
数组的原始二进制数据。这很可能不是任何可以解释为文本的数据。您还使用错误的参数调用fwrite
函数,因此您将读取数组的边界,并具有未定义的行为。如果clientdata
确实是一个数组(而不是指针)然后sizeof(clientdata)
返回数组的实际大小(以字节为单位)。使用sizeof(int)
作为元素大小将导致它写入的数据是实际数据的四倍(如果sizeof(int)
很可能是4
)。第一个size参数是每个元素的大小,因此sizeof(int)
在这里是正确的,但是第二个size是元素的数量(不是字节)您想写。@user3577378 C中的随机序列是伪随机序列。它实际上是预定义的。只要您提供相同的种子
,它将生成相同的随机数序列。我给出了一个答案作为示例。您需要指定随机种子。指定随机种子是什么意思?您调用文件>client.txt
表示它是一个文本文件,但您不编写“文本”但是clientdata
数组的原始二进制数据。这很可能不是任何可以解释为文本的数据。您还使用错误的参数调用fwrite
函数,因此您将读取数组的边界,并具有未定义的行为。如果clientdata
确实是一个数组(而不是指针)然后sizeof(clientdata)
返回数组的实际大小(以字节为单位)。使用sizeof(int)
作为元素大小将导致它写入的数据是实际数据的四倍(如果sizeof(int)
很可能是4
)。第一个size参数是每个元素的大小,因此sizeof(int)
在这里是正确的,但是第二个size是元素的数量(不是字节)你想写。@user3577378 C中的随机序列是一个伪随机序列。它实际上是预定义的。只要你提供相同的种子,它就会生成相同的随机数序列。我举了一个答案作为例子,这就是我要找的for@user3577378是的,知道生成的随机数是伪随机数dom编号是关键。:)这就是我要找的for@user3577378是的,知道生成的随机数是伪随机数是关键。:)