我正在研究一个一维细胞自动机程序,它需要一串 20 个 1 和 0。现在,我有一个常量字符串,每次都以相同的方式启动程序。我的问题:如何随机化字符串,以便每次运行程序时得到不同的结果?任何帮助,将不胜感激。
到目前为止,这是我的代码:
#include <iostream>
#include <bitset>
#include <string>
#include <ctime>
using namespace std;
const int arrSize = 20;
int numGen = 10;
string Initial = "1001101010110100010";
int main()
{
bitset<arrSize + 2> array(Initial);
for(int i = 0; i < numGen; i++)
{
bitset<arrSize + 2> tempArr(array);
for(int k = arrSize; k >= 1 ; k--)
{
if(array[k])
cout << "1";
else
cout << "0";
int number = (int)array[k-1] << 2 | (int)array[k] << 1 | (int)array[k+1];
tempArr[k] = (number == 3 || number == 5 || number == 6);
}
array = tempArr;
cout << endl;
}
}