为什么 rand 每次都给我几乎相同(但略有不同)的数字
Why is rand giving me almost identical (but slightly different) numbers each time
我用c++写了下面一段代码生成随机数
#include <stdlib.h>
#include <iostream>
#include <ctime>
#define ARRAY_SIZE 5
#define MAX_VAL ARRAY_SIZE*5+1
int main() {
srand(time(NULL));
int arr [ARRAY_SIZE];
for (int i = 0; i < ARRAY_SIZE; i++) {
arr[i] = (rand() % MAX_VAL);
}
for (int i = 0; i < ARRAY_SIZE; i++) {
printf ("%d\n", arr[i]);
}
return 0;
}
当我 运行 时,我每次得到 几乎 相同的数字:
tyler@Tylers-MacBook-Pro hw2 % ./MergeSort
11
16
16
21
16
tyler@Tylers-MacBook-Pro hw2 % ./MergeSort
21
11
21
11
6
tyler@Tylers-MacBook-Pro hw2 % ./MergeSort
6
6
1
16
6
tyler@Tylers-MacBook-Pro hw2 % ./MergeSort
16
1
16
6
21
tyler@Tylers-MacBook-Pro hw2 % ./MergeSort
1
21
21
11
21
tyler@Tylers-MacBook-Pro hw2 % ./MergeSort
1
21
21
11
21
tyler@Tylers-MacBook-Pro hw2 % ./MergeSort
11
16
1
1
11
tyler@Tylers-MacBook-Pro hw2 % ./MergeSort
11
16
1
1
11
tyler@Tylers-MacBook-Pro hw2 % ./MergeSort
21
1
6
6
1
为什么我的随机数生成器只给我值:1、6、11、16 和 21?这对我来说毫无意义。我确保为它播种,但数字并不总是以相同的顺序排列,这让这更加令人困惑。作为旁注,我正在使用 OSX.
问题是 MAX_VAL
被定义为 ARRAY_SIZE*5+1
,而不是 (ARRAY_SIZE*5+1)
。这意味着您在 arr[i] = (rand() % MAX_VAL);
中的使用扩展为:
arr[i] = (rand() % 5 * 5 + 1);
选项不多(只有 5 种可能性),这就是您看到相同数字的原因。您可以通过将 MAX_VAL
的定义括起来或使其成为常量变量来解决此问题:
const unsigned int MAX_VAL = ARRAY_SIZE * 5 + 1;
次要问题是 srand(time(NULL))
的使用。在大多数系统上,time
will return the same value if the program is run in the same second. This means running the program in rapid succession (within the same second) will yield the same results. It is preferable to use the PRNG facilities in <random>
.
这是因为您使用了 #define MAX_VAL
实际计算是rand() % 5 * 5 + 1
,也就是说,你先对rand()的结果取5的模,然后乘以5,然后加1。
我假设你的意思是写 rand () % (5 * 5 + 1)
可以通过以下方式解决:
#define MAX_VAL (ARRAY_SIZE * 5 + 1)
其他人指出了此代码中的两个主要问题,但值得在这里展示 C++ 的做事方式以进行对比,并取消学习首先破坏此代码的许多 C 思想。
此代码的 C++ 版本通过使用 C++ 具有而 C 缺乏的功能回避了这里的许多问题:
#include <random>
#include <vector>
#include <iostream>
int main() {
// Define constants instead of using #define, as this avoids interpolation syntax issues
const size_t array_size = 5;
const int max = array_size * 5 + 1;
// Use the C++ random number generator facilities
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<> dis(0, max);
// Use a dynamically sized array
std::vector<int> arr;
for (int i = 0; i < array_size; ++i) {
arr.push_back(dis(gen));
}
// Use C++ container iteration to simplify code
for (const int& i : arr) {
// Use streams for output
std::cout << i << std::endl;
}
return 0;
}
我用c++写了下面一段代码生成随机数
#include <stdlib.h>
#include <iostream>
#include <ctime>
#define ARRAY_SIZE 5
#define MAX_VAL ARRAY_SIZE*5+1
int main() {
srand(time(NULL));
int arr [ARRAY_SIZE];
for (int i = 0; i < ARRAY_SIZE; i++) {
arr[i] = (rand() % MAX_VAL);
}
for (int i = 0; i < ARRAY_SIZE; i++) {
printf ("%d\n", arr[i]);
}
return 0;
}
当我 运行 时,我每次得到 几乎 相同的数字:
tyler@Tylers-MacBook-Pro hw2 % ./MergeSort
11
16
16
21
16
tyler@Tylers-MacBook-Pro hw2 % ./MergeSort
21
11
21
11
6
tyler@Tylers-MacBook-Pro hw2 % ./MergeSort
6
6
1
16
6
tyler@Tylers-MacBook-Pro hw2 % ./MergeSort
16
1
16
6
21
tyler@Tylers-MacBook-Pro hw2 % ./MergeSort
1
21
21
11
21
tyler@Tylers-MacBook-Pro hw2 % ./MergeSort
1
21
21
11
21
tyler@Tylers-MacBook-Pro hw2 % ./MergeSort
11
16
1
1
11
tyler@Tylers-MacBook-Pro hw2 % ./MergeSort
11
16
1
1
11
tyler@Tylers-MacBook-Pro hw2 % ./MergeSort
21
1
6
6
1
为什么我的随机数生成器只给我值:1、6、11、16 和 21?这对我来说毫无意义。我确保为它播种,但数字并不总是以相同的顺序排列,这让这更加令人困惑。作为旁注,我正在使用 OSX.
问题是 MAX_VAL
被定义为 ARRAY_SIZE*5+1
,而不是 (ARRAY_SIZE*5+1)
。这意味着您在 arr[i] = (rand() % MAX_VAL);
中的使用扩展为:
arr[i] = (rand() % 5 * 5 + 1);
选项不多(只有 5 种可能性),这就是您看到相同数字的原因。您可以通过将 MAX_VAL
的定义括起来或使其成为常量变量来解决此问题:
const unsigned int MAX_VAL = ARRAY_SIZE * 5 + 1;
次要问题是 srand(time(NULL))
的使用。在大多数系统上,time
will return the same value if the program is run in the same second. This means running the program in rapid succession (within the same second) will yield the same results. It is preferable to use the PRNG facilities in <random>
.
这是因为您使用了 #define MAX_VAL
实际计算是rand() % 5 * 5 + 1
,也就是说,你先对rand()的结果取5的模,然后乘以5,然后加1。
我假设你的意思是写 rand () % (5 * 5 + 1)
可以通过以下方式解决:
#define MAX_VAL (ARRAY_SIZE * 5 + 1)
其他人指出了此代码中的两个主要问题,但值得在这里展示 C++ 的做事方式以进行对比,并取消学习首先破坏此代码的许多 C 思想。
此代码的 C++ 版本通过使用 C++ 具有而 C 缺乏的功能回避了这里的许多问题:
#include <random>
#include <vector>
#include <iostream>
int main() {
// Define constants instead of using #define, as this avoids interpolation syntax issues
const size_t array_size = 5;
const int max = array_size * 5 + 1;
// Use the C++ random number generator facilities
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<> dis(0, max);
// Use a dynamically sized array
std::vector<int> arr;
for (int i = 0; i < array_size; ++i) {
arr.push_back(dis(gen));
}
// Use C++ container iteration to simplify code
for (const int& i : arr) {
// Use streams for output
std::cout << i << std::endl;
}
return 0;
}