生成随机数出错
Generating random numbers went wrong
我有这个代码。我正在生成 2 个带有随机数的数组,然后使用 arrayToString
函数从这些数组中创建 2 个字符串,但我的输出很奇怪。
class job1Instance : public pp::Instance {
public:
explicit job1Instance(PP_Instance instance): pp::Instance(instance) {}
virtual ~job1Instance() {}
virtual void HandleMessage(const pp::Var& message) {
// declare all the zises
int32_t minNum = 1;
int32_t maxNum = 100;
int32_t arrayElements = maxNum;
// the arrays
int32_t unsorted1[arrayElements/2];
int32_t unsorted2[arrayElements/2];
// fill the arrays with random numbers
unsortedArrays(unsorted1, unsorted2, arrayElements, minNum, maxNum);
std::string outRes1, outRes2, jsonStr;
arrayToString(unsorted1, arrayElements/2, outRes1);
arrayToString(unsorted2, arrayElements/2, outRes2);
PostMessage(pp::Var(outRes2));
}
private:
// function to create a random number between min and max
int32_t rangeRandomAlg (int32_t min, int32_t max) {
int32_t num = max - min + 1;
int32_t remainder = RAND_MAX % num;
int32_t x;
do {
x = rand();
} while (x >= RAND_MAX - remainder);
return min + x % num;
}
// function to create arrays with random numbers
void unsortedArrays (int32_t unsorted1[], int32_t unsorted2[], int32_t arrayElements, int32_t &minNum, int32_t &maxNum) {
for(int32_t i = 0; i < arrayElements; i++) {
if (i < arrayElements/2) {
//unsorted1[i] = rangeRandomAlg(minNum, maxNum);
unsorted1[i] = rand() % maxNum + minNum;
} else {
//unsorted2[i] = rangeRandomAlg(minNum, maxNum);
unsorted2[i] = rand() % maxNum + minNum;
}
}
}
// convert the arrays to string
void arrayToString (int32_t array[], int32_t arraySize, std::string& arrayString) {
for (int i = 0; i <= arraySize; ++i){
arrayString+= std::to_string(array[i]);
if (i != arraySize) {
arrayString+= ',';
}
}
}
有人能告诉我为什么我的 outRes2
输出有那些数字吗?
-18700984,-18701112,8,0,2,0,-66124,0,-66124,0,267757568,0,-65608,0,1,0,-65608,0,266960448,0,-66124,0,1,0,-18699984,0,-66124,0,-18699984,0,266959840,0,7,-66124,-18699984,0,-66124,0,-68200,0,-18699984,0,266959360,0,1,0,536870911,0,-18700016,0,91
它们显然不是我minNum
和maxNum
定义的1到100之间,我找不到问题。
请不要使用 rand(),使用现代版本...
它让一切变得更容易!
这是一个简单的示例代码:
#include <random>
#include <iostream>
#include <string>
int main(){
std::random_device now;
std::mt19937 engine(now());
std::uniform_real_distribution<double> r(0, 100); //!
std::string str = std::to_string( r(engine) );
std::cout << str << std::endl;
system("pause");
return 0;
}
您声明了两个数组,每个数组的大小为 arrayElements/2
:
int32_t unsorted1[arrayElements/2];
int32_t unsorted2[arrayElements/2];
您的循环按如下方式初始化它们:
if (i < arrayElements/2) {
//unsorted1[i] = rangeRandomAlg(minNum, maxNum);
unsorted1[i] = rand() % maxNum + minNum;
} else {
//unsorted2[i] = rangeRandomAlg(minNum, maxNum);
unsorted2[i] = rand() % maxNum + minNum;
}
因此,例如当i
的值达到arrayElements/2
时,if
语句的else
部分将执行:
unsorted2[arrayElements/2] = rand() % maxNum + minNum;
因为 unsorted2
的大小是 arrayElements/2
,这个数组只包含值 unsorted2[0]
到 unsorted2[arrayElements/2-1]
,并且这个赋值超出了数组的末尾,结果在未定义的行为中。
我有这个代码。我正在生成 2 个带有随机数的数组,然后使用 arrayToString
函数从这些数组中创建 2 个字符串,但我的输出很奇怪。
class job1Instance : public pp::Instance {
public:
explicit job1Instance(PP_Instance instance): pp::Instance(instance) {}
virtual ~job1Instance() {}
virtual void HandleMessage(const pp::Var& message) {
// declare all the zises
int32_t minNum = 1;
int32_t maxNum = 100;
int32_t arrayElements = maxNum;
// the arrays
int32_t unsorted1[arrayElements/2];
int32_t unsorted2[arrayElements/2];
// fill the arrays with random numbers
unsortedArrays(unsorted1, unsorted2, arrayElements, minNum, maxNum);
std::string outRes1, outRes2, jsonStr;
arrayToString(unsorted1, arrayElements/2, outRes1);
arrayToString(unsorted2, arrayElements/2, outRes2);
PostMessage(pp::Var(outRes2));
}
private:
// function to create a random number between min and max
int32_t rangeRandomAlg (int32_t min, int32_t max) {
int32_t num = max - min + 1;
int32_t remainder = RAND_MAX % num;
int32_t x;
do {
x = rand();
} while (x >= RAND_MAX - remainder);
return min + x % num;
}
// function to create arrays with random numbers
void unsortedArrays (int32_t unsorted1[], int32_t unsorted2[], int32_t arrayElements, int32_t &minNum, int32_t &maxNum) {
for(int32_t i = 0; i < arrayElements; i++) {
if (i < arrayElements/2) {
//unsorted1[i] = rangeRandomAlg(minNum, maxNum);
unsorted1[i] = rand() % maxNum + minNum;
} else {
//unsorted2[i] = rangeRandomAlg(minNum, maxNum);
unsorted2[i] = rand() % maxNum + minNum;
}
}
}
// convert the arrays to string
void arrayToString (int32_t array[], int32_t arraySize, std::string& arrayString) {
for (int i = 0; i <= arraySize; ++i){
arrayString+= std::to_string(array[i]);
if (i != arraySize) {
arrayString+= ',';
}
}
}
有人能告诉我为什么我的 outRes2
输出有那些数字吗?
-18700984,-18701112,8,0,2,0,-66124,0,-66124,0,267757568,0,-65608,0,1,0,-65608,0,266960448,0,-66124,0,1,0,-18699984,0,-66124,0,-18699984,0,266959840,0,7,-66124,-18699984,0,-66124,0,-68200,0,-18699984,0,266959360,0,1,0,536870911,0,-18700016,0,91
它们显然不是我minNum
和maxNum
定义的1到100之间,我找不到问题。
请不要使用 rand(),使用现代版本... 它让一切变得更容易! 这是一个简单的示例代码:
#include <random>
#include <iostream>
#include <string>
int main(){
std::random_device now;
std::mt19937 engine(now());
std::uniform_real_distribution<double> r(0, 100); //!
std::string str = std::to_string( r(engine) );
std::cout << str << std::endl;
system("pause");
return 0;
}
您声明了两个数组,每个数组的大小为 arrayElements/2
:
int32_t unsorted1[arrayElements/2];
int32_t unsorted2[arrayElements/2];
您的循环按如下方式初始化它们:
if (i < arrayElements/2) {
//unsorted1[i] = rangeRandomAlg(minNum, maxNum);
unsorted1[i] = rand() % maxNum + minNum;
} else {
//unsorted2[i] = rangeRandomAlg(minNum, maxNum);
unsorted2[i] = rand() % maxNum + minNum;
}
因此,例如当i
的值达到arrayElements/2
时,if
语句的else
部分将执行:
unsorted2[arrayElements/2] = rand() % maxNum + minNum;
因为 unsorted2
的大小是 arrayElements/2
,这个数组只包含值 unsorted2[0]
到 unsorted2[arrayElements/2-1]
,并且这个赋值超出了数组的末尾,结果在未定义的行为中。