c_cpp 使用细胞自动机生成随机数
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp 使用细胞自动机生成随机数相关的知识,希望对你有一定的参考价值。
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
static const unsigned char CELL_RULE_BIN[] = {
22, 87, 97, 111, 107, 103, 223, 68, 3, 96, 211, 220, 246,
75, 60, 88, 2, 35, 26, 50, 174, 134, 53, 78, 98, 149, 190, 214, 155, 70,
237, 1, 48, 251, 63, 173, 207, 180, 200, 79, 17, 121, 15, 151, 61, 189, 56,
247, 146, 42, 157, 99, 36, 226, 218, 10, 159, 243, 115, 167, 89, 95, 206, 125,
74, 51, 130, 187, 224, 201, 184, 58, 162, 136, 227, 106, 32, 118, 141, 239, 82,
166, 176, 175, 203, 85, 165, 104, 59, 128, 156, 76, 31, 117, 16, 24, 112, 6, 83,
163, 116, 123, 91, 110, 230, 55, 120, 7, 221, 161, 147, 234, 4, 225, 27, 133, 108, 72,
23, 45, 64, 171, 28, 241, 81, 39, 49, 236, 73, 14, 232, 138, 113, 135, 169, 54, 62, 144,
204, 219, 192, 86, 8, 65, 233, 170, 185, 193, 126, 129, 52, 41, 67, 212, 238, 158, 137, 9,
84, 177, 209, 139, 178, 20, 44, 18, 93, 47, 213, 153, 150, 229, 100, 46, 205, 255, 208, 194,
228, 119, 69, 197, 66, 188, 215, 34, 109, 198, 148, 186, 92, 90, 249, 140, 145, 222, 43, 29, 217,
244, 102, 0, 132, 127, 30, 152, 240, 143, 71, 254, 168, 25, 183, 154, 11, 248, 40, 37, 253, 252,
122, 57, 210, 38, 231, 195, 216, 142, 196, 179, 131, 250, 94, 124, 191, 101, 172, 80, 21, 77, 33, 105,
13, 202, 114, 12, 19, 5, 199, 245, 242, 181, 160, 182, 235, 164
};
unsigned long cellular_random(unsigned long base, int cycles)
{
unsigned char* cell = (unsigned char*)(&base);
while(cycles--)
{
unsigned i;
unsigned char check;
for(i = 0 ; i < sizeof(unsigned long); i++)
{
if(i == sizeof(unsigned long) - 1) {
check = cell[0];
} else {
check = cell[i + 1];
}
cell[i] = CELL_RULE_BIN[check];
}
cell = (unsigned char*)(&base); // reset
}
return base;
}
int main(int argc, char const *argv[])
{
unsigned long start = 5381;
for(int i = 1 ; i < 20 ; i++) {
printf("new random number: %lu\n", cellular_random(start, i));
}
// seed by time
time_t time_seed;
time_seed = time(NULL);
unsigned long utime_seed = (unsigned long)time_seed;
for(int i = 1 ; i < 20 ; i++) {
printf("new time seed random number: %lu\n", cellular_random(utime_seed, i));
}
return 0;
}
以上是关于c_cpp 使用细胞自动机生成随机数的主要内容,如果未能解决你的问题,请参考以下文章
c_cpp xoroshiro128 +伪随机数生成器的C ++包装器
c_cpp 伪随机生成的假数据用于测试内部工具
c_cpp C中的小型随机字符串生成器
c_cpp 自动生成的对象描述
怎样用Excel随机函数rand()生成随机数字和大写字母,能自动产生4位混合的
细胞自动机