我正在测试random_shuffle函数模板,并且能够在我的MacBook上生成不同的输出。但是,当我在我朋友的Windows机器上试用它时会有所不同。
该程序将尝试生成5 int
并随机交换它们。它后来转换为char
用于其他一些用途,这并不重要。
randPerm()
函数将初始化vector
并执行交换,然后作为数组返回。由于上面定义的POPULATION_SIZE常量,此函数将被调用2次。之后,chromosome
2D数组将填充相应的字符。
#include <iostream>
#include <algorithm>
#include <iterator>
#include <vector>
#include <cstdlib>
using namespace std;
// constant
const int NUM_OF_GENE = 5;
const int POPULATION_SIZE = 2;
const char CITIES[NUM_OF_GENE] = { 'A', 'B', 'C', 'D', 'E' };
// chromosome structure
char chromosome[POPULATION_SIZE][NUM_OF_GENE];
// function prototypes
int generateSeed(int i);
int * randPerm();
void initializeChromosome();
void printChromosome();
int main(int argc, const char * argv[]) {
initializeChromosome();
printChromosome();
return 0;
}
// seed generator
int generateSeed(int i) {
return rand() % i;
}
// return a randomly permutated array
int * randPerm() {
static int arr[NUM_OF_GENE];
// give some randomness
srand ( unsigned ( time(0) ) );
vector<int> myVector;
for (int i=0; i<NUM_OF_GENE; i++) {
myVector.push_back(i);
}
random_shuffle(myVector.begin(), myVector.end());
random_shuffle(myVector.begin(), myVector.end(), generateSeed);
copy(myVector.begin(), myVector.end(), arr);
return arr;
}
void initializeChromosome() {
for (int i=0; i<POPULATION_SIZE; i++) {
int * randArr = randPerm();
// allocate cities (in char) into the chromosome array
for (int j=0; j<NUM_OF_GENE; j++) {
chromosome[i][j] = CITIES[randArr[j]];
}
}
}
void printChromosome() {
for (int i=0; i<POPULATION_SIZE; i++) {
for (int j=0; j<NUM_OF_GENE; j++) {
cout << chromosome[i][j] << " ";
}
cout << "\n";
}
}
Windows计算机上的结果
甚至使用CodeBlocks,Visual Studio 2017甚至在线c ++编译器进行了测试。还使用不同的GCC
版本进行了测试。我的Mac正在使用GCC 4.2.1
B A D E C
B A D E C
答案 0 :(得分:2)
HansPassant的建议很有帮助。通过从循环中删除srand()
函数,我可以再次实现随机性。
#include <iostream>
#include <algorithm>
#include <iterator>
#include <vector>
#include <cstdlib>
using namespace std;
// constant
const int NUM_OF_GENE = 5;
const int POPULATION_SIZE = 2;
const char CITIES[NUM_OF_GENE] = { 'A', 'B', 'C', 'D', 'E' };
//const int CITIES[NUM_OF_GENE] = {0, 1, 2, 3, 4};
// chromosome structure
char chromosome[POPULATION_SIZE][NUM_OF_GENE];
// function prototypes
int generateSeed(int i);
int * randPerm();
void initializeChromosome();
void printChromosome();
int main(int argc, const char * argv[]) {
srand ( unsigned ( time(0) ) );
initializeChromosome();
printChromosome();
return 0;
}
int generateSeed(int i) {
return rand() % i;
}
// return a randomly permutated array
int * randPerm() {
static int arr[NUM_OF_GENE];
vector<int> myVector;
for (int i=0; i<NUM_OF_GENE; i++) {
myVector.push_back(i);
}
random_shuffle(myVector.begin(), myVector.end());
random_shuffle(myVector.begin(), myVector.end(), generateSeed);
copy(myVector.begin(), myVector.end(), arr);
return arr;
}
void initializeChromosome() {
for (int i=0; i<POPULATION_SIZE; i++) {
int * randArr = randPerm();
// allocate cities (in char) into the chromosome array
for (int j=0; j<NUM_OF_GENE; j++) {
chromosome[i][j] = CITIES[randArr[j]];
}
}
}
void printChromosome() {
for (int i=0; i<POPULATION_SIZE; i++) {
for (int j=0; j<NUM_OF_GENE; j++) {
cout << chromosome[i][j] << " ";
}
cout << "\n";
}
}