[英]C++ Random Number Generator is not acting random
我有一些C ++代碼正在上面調用另一個函數。 該功能依賴於其選擇之一的隨機性,但並非隨機起作用。 每次都找到相同的數字。 我如何保證它是隨機的?
我嘗試在代碼開頭調用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);
}
}
}
在稱為后繼函數的函數中生成隨機數時,它將總是一遍又一遍地找到相同的數字,這破壞了它的功能。
在程序中播種一次隨機數生成器,而不是每次都想生成隨機數!
int main()
{
std::srand( /* some random source */ );
run_rest_of_program();
}
隨機性的來源可能是像std::time(nullptr)
這樣天真的東西(嬰兒的第一顆種子),或更合適的像是std::random_device{}()
那樣的東西。 對於后者, #include <random>
。
無論如何,您都應該使用C ++的<random>
工具 ,而不是不良的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';
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.