Re: /dev/random vs. /dev/urandom

linux-os
2005-01-07T19:31:51Z

On Fri, 7 Jan 2005, Ron Peterson wrote:

> When I compile and run the code below, the string of octal characters
> generated by reading /dev/random contains long strings of zeroes. I was
> under the impression that /dev/random is "more random" than
> /dev/urandom, and will block when it runs out of entropy until it
> gathers more. It's only when RAND_LEN is on the largish side that these
> strings of zeroes appear.
>
> I'm guessing this behaviour is to be expected; I just don't understand
> it.
>
> This is linux kernel 2.6.8.1.
>
> #include <stdio.h>
> #include <fcntl.h>
> #define RAND_LEN 1024

I don't understand your code. Using standard Unix tools, results
in the expected behavior:

Script started on Fri 07 Jan 2005 02:19:00 PM EST
LINUX> od -x /dev/random
0000000 43ba fd1e 2a04 4339 2b7e e55a 6e54 71c2
0000020 6102 9518 d0e9 5f9e b4ed 1ccc 21a1 078c
0000040 8d91 c70c 9ef0 c053 fce2 cb8c f890 1306
0000060 138c a669 8bdd af97 629b 324f b771 2f7a
0000100 0a8b 9c58 d131 db14 6350 1fde d4b7 b775
0000120 d16a 77f8 a201 abe1 a2fa 364c d773 2a63
0000140 526b 6042 7a83 f457 58ca 016c 780f 7506
0000160 d564 216d e325 86d0 5c82 a180 b835 3bd3
0000200 0c6b 90f1 0f15 823d 2df7 e8fa d217 5b21
0000220 aabe 8ff2 3f1f 8557 581e 8479 9863 202b
0000240 95e3 d007 7583 d0f4 3fbc b790 f922 4013
0000260 6e16 5a37 0465 4733 2a03 ce2a bb8e c1ee
0000300 f359 f087 798e 0c1d ac02 e6e9 8cb4 e905
0000320 21cc 1816 fd9c f72d 10e7 6195 59cb f40d
0000340 868b 11ef 8a1e a45c e162 acb9 caca 2355
0000360 a3a3 80e9 5f96 87a2 d89c af59 23e3 1918
0000400 58c3 a823 d8b2 e74b 4af0 0fcd bba4 5919
0000420 9ffc 9e74 7ec6 9272 ccdd ba80 ed8e 33c9
0000440 60d3 14eb aa6d 589d bea6 4244 6892 640f
0000460 bf98 5397 3408 1fb3 40fe 3def 4277 663a
0000500 ae94 00d4 fa55 8cd0 8218 4901 1b95 5fd9
0000520 8361 7a55 7cb9 69c8 3c14 9d78 71e5 deba
0000540 7c74 65cf fa62 758a c686 07fb d300 e62d
0000560 b130 792a 9bcc 5652 2d20 d875 852a ede8
0000600 9141 201b 6e54 1271 e5e9 b87e 3f8a 9de9
0000620 8c85 ef50 bcc1 b09a 87aa 46fc d371 06f1
0000640 8345 e3ec bdb6 d97f 462e 9021 90d4 0100
0000660 98bb 5e75 4871 8818 21a3 8082 6844 2845
0000700 efae 3fee 9f6c 1b64 146e 4234 4c39 8934
0000720 1668 920b 5d3b 896f 344e 01d8 7a85 dba1
0000740 cae7 752b 5691 ce82 a6d1 c991 a648 d55a
0000760 afbb bf02 c8c0 a4c4 4d58 fce5 fa87 fe04
0001000 5459 5fad b930 1e3d e25d d5da 25ca 7c16
0001020 a446 f60d b2cc a679 5f57 b9b0 8a0e 24f7
0001040 01a3 65a3 ce7f 1855 f5b6 f9bc 888d b670
0001060 9771 c8d1 2e9b 8ea5 a95a ff33 78a5 695a
0001100 c738 0707 0f8e 8403 ca41 b272 f7a8 7727

LINUX> od -x /dev/urandom
0000000 1647 22fa d1eb 2fa6 99a6 c2f1 9660 d855
0000020 c9df 6118 3774 f9d4 c9bd 619d 3f11 b092
0000040 713b 0c07 d578 7b5e 1128 fe54 7252 bf5b
0000060 109e 4c40 ae9f 2d09 66a0 25bf 241b af82
0000100 6065 af28 7785 2ea1 2955 613a 4f38 41bd
0000120 d978 f5de 2886 9565 2eb7 6f96 144d 2b4d
0000140 d43d 745c 9ac1 68ea a804 c76a fd9e b16b
0000160 16f9 918b 393b c469 8a72 cc1d dfaf 62cf
0000200 a6db a208 81f3 bcb0 8748 9c64 e1e9 fdba
0000220 d80d 1531 d7b4 a0ae a13e f0d2 2605 444c
0000240 deaa dfbc ab47 21af 3b9c 6a63 1ca5 6d38
0000260 b3fc 8ddf ecff 3d76 5a03 5bd7 23ed bca0
0000300 bf60 5fc9 496c 1edb c32d f8fd 5f1e fe96
0000320 8f61 8470 612e df39 495c 91be f9d1 84e3
0000340 a4a3 464a 007a dd81 c121 c59b 536b 426e
0000360 e08a 4cd6 8820 8ca4 c88e b35c 039c aacf
0000400 2a5d 1f2f 0950 5e30 d4bb 7413 4710 65c0
0000420 2a46 98f8 4a4a 6153 2b4b 58d2 8996 9551
0000440 255a 352a 6b06 49d2 5a21 431b 51af 7768
0000460 9f24 3fdf 9e7c 0555 8cf7 f471 f89e 811a
0000500 ae5f e487 8a1f d51a dadf d179 5f48 6f49
0000520 49e5 dc68 0df7 571e 1d44 4662 dcff 1a56
0000540 8986 6dd3 10f5 c893 d9cf d5cb ac51 95e3
0000560 1486 5448 5960 9cd7 0420 b6bb fa00 4726
0000600 e7e3 5d7e 5470 d042 0ed0 9c94 9ade 1380
0000620 1106 f59c 7278 03f2 45c9 41ab 89e9 5d5b
0000640 1b8f 50cf e19a 87c1 cdd0 441a 0358 7f79
0000660 bca3 746f fa8b 0bca c367 1f02 a7a6 a49b
0000700 dd8c 8125 9e8a 57c8 3206 2359 78ca 3a53
0000720 aa24 be40 3859 ad64 c311 ef49 5ae3 ea69
0000740 5f23 c3d8 2b8a e809 9ce7 32b5 e7ae f5b4
0000760 ba39 db5d 68e9 9b6f 052d 0e87 038e 431b
0001000 c9e2 dd73 91ab f330 9888 d65e a85a aa5e
0001020 a825 48a1 6b1d 1995 a4d3 7351 35c8 0923
0001040 d005 75f9 a381 08cc 3c9b da16 bbd1 4a01
0001060 150d 4fcb a7b9 c4d9 3677 96d2 c094 b7bd
0001100 0355 902f 0c03 d7ba f8c2 1010 d77d b865
0001120 8b65 5907 f11b 097e a446 e219 6ed6 dbc7
0001140 f95b 3279 7f6b 6331 6cde 1997 5d64 3231
0001160 88c3 59e7 8823 b026 a6ac 9d6e 5f81 1916
^C

LINUX> exit
Script done on Fri 07 Jan 2005 02:19:40 PM EST

You can't just AND-off a bunch of bits and claim that
the random-number generator doesn't work!

Cheers,
Dick Johnson
Penguin : Linux version 2.6.10 on an i686 machine (5537.79 BogoMips).
Notice : All mail here is now cached for review by Dictator Bush.
98.36% of all statistics are fiction.