我有一些C ++代码调用它上面的另一个函数。该函数取决于其中一个选择的随机性,但它不是随机的。它每次都找到相同的数字。我如何保证它是随机的?
我尝试在代码开头调用 srand ,但我没有帮助。也许我不理解 srand 的重要内容。
这是代码,我知道它很多,但最重要的东西是最顶层的:
vector<vector<int> > Successor(vector<int> sudoku_state) {
srand (time(NULL));
//Core successor function logic: I choose one of the non-unchangable states at random, and change it to any of its other 3 possible values. Eg. If a cell with a '1' is chosen at random, then I return the sudoku board with a '2' a '3' and a '4' at that location, in a vector of vectors.
vector<vector<int> > list_of_all_successors;
int random_indice;
random_indice = rand()%16;
//make sure it is not one of the unchangable squares
while((std::find(unchangables.begin(), unchangables.end(), random_indice) != unchangables.end()))
{
random_indice = rand()%16;
}
if(sudoku_state[random_indice]==1) {
sudoku_state[random_indice]=2;
list_of_all_successors.push_back(sudoku_state);
sudoku_state[random_indice]=3;
list_of_all_successors.push_back(sudoku_state);
sudoku_state[random_indice]=4;
list_of_all_successors.push_back(sudoku_state);
}
else if(sudoku_state[random_indice]==2) {
sudoku_state[random_indice]=1;
list_of_all_successors.push_back(sudoku_state);
sudoku_state[random_indice]=3;
list_of_all_successors.push_back(sudoku_state);
sudoku_state[random_indice]=4;
list_of_all_successors.push_back(sudoku_state);
}
else if(sudoku_state[random_indice]==3) {
sudoku_state[random_indice]=1;
list_of_all_successors.push_back(sudoku_state);
sudoku_state[random_indice]=2;
list_of_all_successors.push_back(sudoku_state);
sudoku_state[random_indice]=4;
list_of_all_successors.push_back(sudoku_state);
}
else { //then we know == 4
sudoku_state[random_indice]=2;
list_of_all_successors.push_back(sudoku_state);
sudoku_state[random_indice]=3;
list_of_all_successors.push_back(sudoku_state);
sudoku_state[random_indice]=1;
list_of_all_successors.push_back(sudoku_state);
}
return list_of_all_successors;
}
以下是来电者:
//The core hillclimbing functionality. Performs the logic of the hillclimbing algorithm on the given initial sudoku board, called initial_state. initial_state is represented as a vector of int of size 16. The returned value called goal_state, is also a vector of int of size 16.
vector<int> hillClimber(vector<int> initial_state) {
vector<int> goal_state; // the final goal state
vector<int> sudoku_config_with_lowest_flaws = initial_state; //the state being worked with in each iteration
int minimum = 500; //the current minimum amount of flaws found in a state (set to 500 initially as a max)
int iterations = 0; //if iterations reaches a high amount, random restart.
while(true) {
cout << "\nBeginning of hillClimber loop reached.\n";
vector<vector<int> > all_successors = Successor(sudoku_config_with_lowest_flaws);
cout << "Potential Successors to beginning state determined. Now analyzing. \n";
//****Core hillClimber logic:
//Loop over the successors and determine one with lowest amount of flaws.
int j = 0;
while(j<all_successors.size()) {
vector<int> next_state = all_successors[j];
int number_of_flaws = Evaluator(next_state);
if(number_of_flaws <= minimum) {
minimum = number_of_flaws;
sudoku_config_with_lowest_flaws = next_state;
}
if(number_of_flaws==0) {
//print the final solution
cout << "**********A SOLUTION HAS BEEN DETERMINED**********\n";
for(int i=0; i < sudoku_config_with_lowest_flaws.size(); i++){
cout << sudoku_config_with_lowest_flaws[i];
if(i==3 || i==7 || i==11)
cout << '\n';
}
return sudoku_config_with_lowest_flaws;
}
j++;
}
//print the next lowest state configuration found:
cout << "Next configuration (passed into next loop of hillClimber) shown below: (Still has " << minimum << " flaws). \n";
for(int i=0; i < sudoku_config_with_lowest_flaws.size(); i++){
cout << sudoku_config_with_lowest_flaws[i];
if(i==3 || i==7 || i==11)
cout << '\n';
}
iterations++;
if(iterations>50) {
//randomized restart area: re-randomizes the initial_state, and runs again.
cout << "\n ***Reached a local min -- Had to restart hillClimber with a new initial_state! *** \n";
int i = 0;
for(i = 0; i<initial_state.size(); i++) {
if(!(std::find(unchangables.begin(), unchangables.end(), i) != unchangables.end())) {
initial_state[i] = rand() % 4 + 1;
}
}
cout << "New initial state: \n";
for(int i=0; i < initial_state.size(); i++){
cout << initial_state[i];
if(i==3 || i==7 || i==11)
cout << '\n';
}
cout << "\n Press Enter to continue \n";
getchar();
return hillClimber(initial_state);
}
}
}
在名为Successor的函数中的随机数生成中,它总是会反复找到相同的数字,这会破坏它的功能。
答案 0 :(得分:3)
在程序中将随机数生成器一次播种,而不是每次都要生成随机数!
int main()
{
std::srand( /* some random source */ );
run_rest_of_program();
}
随机性的来源可能是天真的,如std::time(nullptr)
(宝宝的第一粒种子),或更恰当的像std::random_device{}()
。对于后者,#include <random>
。
无论如何,你应该使用C++'s <random>
facilities,而不是穷人的C rand()
。例如:
#include <iostream>
#include <random>
int main()
{
std::mt19937 rng(std::random_device{}()); // the PRNG
std::normal_distribution<double> dist; // a distribution
// Print 100 standard-normally distributed numbers.
for (int i = 0; i != 100; ++i)
{
std::cout << dist(rng) << '\n';
}
}