Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/57.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 从dev/urandom获取真正随机的数据_C_Random - Fatal编程技术网

C 从dev/urandom获取真正随机的数据

C 从dev/urandom获取真正随机的数据,c,random,C,Random,我在一个循环中阅读dev/uradom,为模拟创建随机数。它不起作用,我最终跟踪到了下面的代码示例 int myFile = open("/dev/urandom", O_RDONLY); begin outer loop for (ii = 0 ; ii<12 ; ii++ ) { read(myFile, &rand, 2) ; printf("%d " , (int) rand) ; } 我是不是做错了什么 是

我在一个循环中阅读dev/uradom,为模拟创建随机数。它不起作用,我最终跟踪到了下面的代码示例

int myFile = open("/dev/urandom", O_RDONLY);
begin outer loop
for (ii = 0 ; ii<12 ; ii++ )
    {
            read(myFile, &rand, 2) ;
            printf("%d " , (int) rand) ;
    }
我是不是做错了什么

是的,代码正在使用
rand
而没有首先检查
read(myFile,&rand,2)的返回值


代码读取有问题,请检查read()的返回值

总是2点吗?我怀疑代码不耐烦-当返回值<2时,代码只需等待并重试,当返回值为1时,可能会保存部分结果。(使用字节数组)


然而,从更大的角度来看,从
“/dev/uradom”
检索大量数据表明,代码应该使用一种新的方法 快速随机数<代码>“/dev/uradom”
需要时间来生成随机位。通常的方法是使用
“/dev/uradom”
中的位来播种质量,然后调用该PRNG

可选地,考虑<代码> /DEV/NODE < <代码> > < /P>



进入更多细节。

最终找到了这一点。似乎重复读取dev/uradom可以重复结果。解决方案似乎是要么关闭文件,重新打开并再次读取,要么从dev/uradom中读取一大块

大多数“随机”函数都是伪随机函数,只能和它们的种子一样好。你能给函数提供一些至少有点随机的东西吗?可能是光标移动,或其他无法跟踪的内容?或者,您可以尝试从
/dev/random
而不是
/dev/uradom
读取,前者在没有足够的熵来提供适当的随机性时会阻塞,后者则不会。看看你读到了什么关于
/dev/uradom
?为什么您使用的是本质上不可靠的生成器,而不是标准的
/dev
random`?提示:一个简单的维基百科搜索就足够了。@jwsc:这基本上就是
/dev/?random
所做的。我读到了关于Uradom的什么?如果没有足够的熵,它应该使用PRNG,这样它至少是非阻塞的几乎随机的数据。然而,这个结果与随机结果完全不同。“似乎从dev/uradom重复读取可以重复结果。”-->当代码收到重复结果时,
read()
的返回值是多少?当返回值为0时,
read(myFile,&rand,2)
没有重复结果,它从未更改过
rand
@chux实际上,它从未返回0。根据你回答中的建议,我确实检查了这一点。
14315 18369 22645 5910 55240 17092 32556 55028 34376 34902 44236 56796
15767 54265 22372 38639 44446 25037 58094 28251 65329 48865 28057 59835
17135 57009 5612 8627 55111 365 9988 62402 42682 57066 59920 10659
21386 60845 9243 27377 18829 10162 64795 55836 53913 39765 51749 47917
21522 30166 44059 30351 15622 17957 43399 26184 44558 4553 41745 54603
2179 11970 21234 39538 45359 50854 58277 62479 4172 14207 20551 54455
27120 22497 51021 41488 51668 50638 13894 37707 19813 40971 52191 27702
29443 20239 59131 12186 24974 55298 56381 50092 11114 55533 11428 17223
30520 32207 42256 31605 32485 3700 47741 25023 50723 64056 44451 25864
30557 2136 55462 20308 35902 30238 21815 9255 48414 1956 27819 49185
34428 59204 1955 10782 20077 59336 27867 29254 46219 28142 19108 52866
39392 30683 40700 11760 7835 65096 22363 41318 9363 39016 34225 20539
39956 47480 12499 49225 15360 18322 38157 61226 43010 39099 9631 49289
40243 53645 10750 40712 32496 53093 2925 36166 60770 45263 30683 2537
41922 56658 30762 49046 64446 11102 53481 43032 11576 31789 17015 47093
44196 65248 30185 28250 27134 56704 42954 18203 36677 21296 22780 27224
50771 19717 34434 16434 43648 57975 64581 51131 748 21266 26116 29653
52032 13956 15378 8284 47951 24566 58583 61174 53515 1697 8710 46559
54603 54603 54603 54603 54603 54603 54603 54603 54603 54603 54603 54603
54603 54603 54603 54603 54603 54603 54603 54603 54603 54603 54603 54603
54603 54603 54603 54603 54603 54603 54603 54603 54603 54603 54603 54603
55683 26712 63891 36499 40089 58448 32114 14948 57516 35102 23239 45829
62055 29107 10766 37551 30467 33145 4124 46208 60329 54800 29127 53398
62990 41904 30506 58700 13996 24915 18279 54703 8346 29550 2794 19473
64367 38250 23821 41639 16940 5076 26526 26796 23550 58420 10689 18614
7471 34008 62429 27549 53162 37714 26394 37498 14193 15684 31649 31621
7614 16389 59721 27793 58153 9808 61092 15071 42413 57890 33708 5530
if (read(myFile, &rand, 2) != 2) Handle_It();
else printf("%d " , (int) rand) ;