首页
社区
课程
招聘
利用热力学第二定律建造真随机数
发表于: 2018-12-19 19:13 7584

利用热力学第二定律建造真随机数

2018-12-19 19:13
7584
  利用热力学第二定律的另一表述——熵增加原理可以高效的建造出真随机数组。
  数组中的数据达到一定的要求时数据被称为随机数组,首先数组的基本元素是连续的、完整的、均匀的,如果元素有缺失是不能形成随机数组的,表现形式是所有基本元素的数量是相同的,在此基础上就是数组中元素的分布了,顺序排列或部分顺序排列都不是随机的,随机的没有任何规律的排列才是随机数组的特征,真随机也没有什么神秘的只是一些分布状态而已,那就看看如何实现这些排列了,下面是用随机排序建造随机数组的例子:
int n1=101;
unsigned char ch;
int i,j,NN=10240000,NK=1;
unsigned char *str; //定义数组指针
str = new unsigned char[NN+1]; //分配内存
for(i=0;i<NN;i++)
{
str[i]=i%256; //建立顺序数组
}
for(j=0;j<NK;j++) //随机排序的次数NK
{
for(i=0;i<NN;i++) //随机排序
{
n1=i + ( i + n1 + str[i] + str[n1])%(NN-i);
ch=str[i];
str[i]=str[n1];
str[n1]=ch;
}
}
  可以看到,共有10240000个元素,基本元素0-255,每种都有40000个。
  随机排序是这样进行的,在待排序的数据中随机选择一个数n1,并且交换str[i]和str[n1]的数值,循环完成后每个元素的序列都发生了变化,就完成了一次随机排序。
  数据n1 是随机排序的关键,它拼凑了一些变量来实现数据变化的随机性。
  可见NK是决定数组进行了多少次随机排序。我们将分析这些数据来评判这一过程。
现在国际上公认的随机数评价方法是NIST测试共有15种数百个指标,我们的数据在不同的随机排序之后也要使用NIST来检测一下。

10240000字节的NIST检测结果如下:
  1)进行一次随机排序的测试结果
近似熵检测ApproximateEntropy = 0.000000
块内频率测试BlockFrequency = 1.000000
累积和测试CumulativeSums = 0.789027 , 0.497647
离散傅立叶变换测试FFT = 0.000000
频率测试Frequency = 0.602193
线性复杂度检测LinearComplexity = 0.572865
块内最长连续“1”测试 = 0.000000
非重叠模板匹配测试NonOverlappingTemplate抽查[1] [50] [100] [148]= 0.497647 , 0.497647 , 0.497647 , 0.497647 
重叠模板匹配测试OverlappingTemplate = 0.000000
随机偏移测试RandomExcursions八点 = 0.000000,0.000003,0.000012,0.060734,0.710044,0.668883,0.645599,0.932786
随机偏移变量测试RandomExcursionsVariant十八点 = 0.016927,0.006212,0.002274,0.000745,0.000511,0.000443,0.000202,0.000921,0.016430,0.288450,0.482328,0.461011,0.499533,0.727773,0.743010,0.748404,0.789161,0.801676
二元矩阵秩测试Rank = 0.000000
游程测试Runs = 0.744299
串行测试Serial = 0.000000
全局通用统计测试Universal = 0.000001
  2)进行三次随机排序的测试结果
近似熵检测ApproximateEntropy = 0.887813
块内频率测试BlockFrequency = 0.612131
累积和测试CumulativeSums = 0.716513 , 0.641557
离散傅立叶变换测试FFT = 0.719998
频率测试Frequency = 0.399508
线性复杂度检测LinearComplexity = 0.954226
块内最长连续“1”测试 = 0.300848
非重叠模板匹配测试NonOverlappingTemplate抽查[1] [50] [100] [148]= 0.641557 , 0.641557 , 0.641557 , 0.641557 
重叠模板匹配测试OverlappingTemplate = 0.292812
随机偏移测试RandomExcursions八点 = 0.201590,0.101736,0.064311,0.011157,0.196435,0.423448,0.058816,0.324844
随机偏移变量测试RandomExcursionsVariant十八点 = 0.756369,0.774686,0.943432,0.780585,0.950888,0.982856,0.918993,0.264409,0.019754,0.012870,0.036389,0.094576,0.120530,0.160800,0.305725,0.533384,0.697271,0.811989
二元矩阵秩测试Rank = 0.312437
游程测试Runs = 0.682886
串行测试Serial = 0.327414
全局通用统计测试Universal = 0.050151
  3)进行五次随机排序的测试结果
近似熵检测ApproximateEntropy = 0.166503
块内频率测试BlockFrequency = 0.223603
累积和测试CumulativeSums = 0.457242 , 0.571503
离散傅立叶变换测试FFT = 0.675447
频率测试Frequency = 0.889648
线性复杂度检测LinearComplexity = 0.192694
块内最长连续“1”测试 = 0.724479
非重叠模板匹配测试NonOverlappingTemplate抽查[1] [50] [100] [148]= 0.571503 , 0.571503 , 0.571503 , 0.571503 
重叠模板匹配测试OverlappingTemplate = 0.587273
随机偏移测试RandomExcursions八点 = 0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000
随机偏移变量测试RandomExcursionsVariant十八点 = 0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000
二元矩阵秩测试Rank = 0.883535
游程测试Runs = 0.257427
串行测试Serial = 0.494902
全局通用统计测试Universal = 0.477515
  4)进行七次随机排序的测试结果
近似熵检测ApproximateEntropy = 0.273182
块内频率测试BlockFrequency = 0.007362
累积和测试CumulativeSums = 0.181577 , 0.174069
离散傅立叶变换测试FFT = 0.450727
频率测试Frequency = 0.984043
线性复杂度检测LinearComplexity = 0.939927
块内最长连续“1”测试 = 0.017737
非重叠模板匹配测试NonOverlappingTemplate抽查[1] [50] [100] [148]= 0.174069 , 0.174069 , 0.174069 , 0.174069 
重叠模板匹配测试OverlappingTemplate = 0.233910
随机偏移测试RandomExcursions八点 = 0.279335,0.134724,0.171779,0.583415,0.562464,0.462367,0.555043,0.260584
随机偏移变量测试RandomExcursionsVariant十八点 = 0.433906,0.400755,0.313842,0.126340,0.049446,0.125783,0.282657,0.467986,0.922114,0.114497,0.078756,0.200369,0.326141,0.286349,0.209493,0.313842,0.412994,0.269445
二元矩阵秩测试Rank = 0.768957
游程测试Runs = 0.487839
串行测试Serial = 0.580894
全局通用统计测试Universal = 0.323612
  5)进行十三次随机排序的测试结果
近似熵检测ApproximateEntropy = 0.933483
块内频率测试BlockFrequency = 0.996558
累积和测试CumulativeSums = 0.905719 , 0.932290
离散傅立叶变换测试FFT = 0.167783
频率测试Frequency = 0.968093
线性复杂度检测LinearComplexity = 0.130748
块内最长连续“1”测试 = 0.069903
非重叠模板匹配测试NonOverlappingTemplate抽查[1] [50] [100] [148]= 0.932290 , 0.932290 , 0.932290 , 0.932290 
重叠模板匹配测试OverlappingTemplate = 0.181520
随机偏移测试RandomExcursions八点 = 0.685155,0.392273,0.812459,0.648181,0.063320,0.133137,0.054298,0.264821
随机偏移变量测试RandomExcursionsVariant十八点 = 0.867602,0.733741,0.665665,0.857459,0.951282,0.818494,0.889983,0.565011,0.491866,0.216006,0.115459,0.054066,0.078027,0.148902,0.125131,0.115780,0.169925,0.177808
二元矩阵秩测试Rank = 0.709401
游程测试Runs = 0.032304
串行测试Serial = 0.591065
全局通用统计测试Universal = 0.583741

  从结果数据可见,NIST检测的数据是由部分合格到全部合格的发展过程,为什么会有这样的结果,这是自然规律所决定的,也从侧面印证了热力学第二定律的客观性。
  用这种方法形成真随机数组是没有问题的,但效率不高,从上面可见,如果n1 算式中引入性能优良的随机函数,那效率将是非常高的,一般一次就能让NIST检测全部通过。
  其实NIST检测就那么有用吗?没能通过NIST的数据就不能用了吗?实际是否定的,本质是只要是不能推算的未知数据作用是差不多的。
  利用自然定律来实现数据的改造是简单有效的方法,希望你有所帮助或启发。





[招生]科锐逆向工程师培训(2024年11月15日实地,远程教学同时开班, 第51期)

收藏
免费 0
支持
分享
最新回复 (2)
雪    币: 515
活跃值: (3272)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
wem
2
2018-12-20 01:20
0
雪    币: 181
活跃值: (621)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
3
2018-12-20 08:01
0
游客
登录 | 注册 方可回帖
返回
//