Re: urandom skew

From: Theodore Y. Ts'o (tytso@MIT.EDU)
Date: Wed Apr 05 2000 - 22:12:38 EST


   Date: Wed, 5 Apr 2000 17:38:15 -0700 (PDT)
   From: "Robert F. Ross" <rross@recourse.net>

   cactus:~$ ./foo /dev/urandom
   0: 7767 1: 7820 2: 7703 3: 7516 4: 7680 5: 7730 6: 7831 7: 7758 8: 3716
   9: 3877 A: 3796 B: 3891 C: 3790 D: 3866 E: 3911 F: 3817
   cactus:~$ ./foo /dev/urandom
   0: 7733 1: 7723 2: 7747 3: 7833 4: 7807 5: 7721 6: 7595 7: 7606 8: 3771
   9: 3850 A: 3879 B: 3873 C: 3883 D: 3910 E: 3967 F: 3924
   cactus:~$ ./foo /dev/urandom
   0: 7729 1: 7860 2: 7793 3: 7743 4: 7694 5: 7754 6: 7610 7: 7620 8: 3841
   9: 3846 A: 3843 B: 3795 C: 3921 D: 3767 E: 3904 F: 3860
   cactus:~$ uname -a
   Linux cactus 2.2.13 #6 Fri Feb 25 23:10:36 PST 2000 i586 unknown
   cactus:~$

   ... i don't know if this issue has been addressed/known/fixed (i don't
   usually do much with linux), but i thought if it hasn't it should be
   pointed out.. the first bit of a nibble being that much more likely to be
   0 than 1 isn't acceptable for most applications that require a steady
   stream of random numbers, i'd think.

You didn't print the sources of your "foo" program, but I'm not seeing
this reported behaviour here, using a 2.2.12 kernel. (see below).

My guess is that you're probably doing something like this:

        char x;

        read(fd, &x, 1)
        count[x >> 4]++;
        count[x & 0xF]++;

...and forgetting to use a signed variable for 'x'.

                                                - Ted

<tytso@snap.thunk.org> {/home/tytso}, level 2
1024% /tmp/foo < /dev/urandom
count[0] = 4180 count[1] = 4133 count[2] = 3986 count[3] = 4086
count[4] = 4088 count[5] = 4069 count[6] = 4220 count[7] = 4214
count[8] = 4131 count[9] = 4129 count[a] = 4034 count[b] = 4011
count[c] = 4013 count[d] = 4110 count[e] = 3935 count[f] = 4197
<tytso@snap.thunk.org> {/home/tytso}, level 2
1025% /tmp/foo < /dev/urandom
count[0] = 4127 count[1] = 4072 count[2] = 4157 count[3] = 4071
count[4] = 4072 count[5] = 4250 count[6] = 4055 count[7] = 4072
count[8] = 4142 count[9] = 4008 count[a] = 3931 count[b] = 4052
count[c] = 4043 count[d] = 4185 count[e] = 4216 count[f] = 4083
<tytso@snap.thunk.org> {/home/tytso}, level 2
1025% /tmp/foo < /dev/urandom
count[0] = 4203 count[1] = 4022 count[2] = 4246 count[3] = 4061
count[4] = 4092 count[5] = 4086 count[6] = 4109 count[7] = 4041
count[8] = 4063 count[9] = 4120 count[a] = 4073 count[b] = 4010
count[c] = 4172 count[d] = 4055 count[e] = 4073 count[f] = 4110
<tytso@snap.thunk.org> {/home/tytso}, level 2
1025% uname -a
Linux snap.thunk.org 2.2.12-20smp #6 SMP Sat Feb 12 23:50:23 EST 2000 i686 unknown

----------------------- /tmp/foo.c

main()
{
        int count[16];
        int i;
        unsigned char ch;

        memset(count, 0, sizeof(count));

        for (i = 0; i < 65536; i++) {
                if (read(0, &ch, 1) != 1)
                        perror("read");
                count[ch >> 4]++;
        }
        for (i=0; i < 16; i++) {
                printf("count[%x] = %4d ", i, count[i]);
                if ((i % 4) == 3)
                        printf("\n");
        }
        printf("\n");
}

-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.rutgers.edu
Please read the FAQ at http://www.tux.org/lkml/



This archive was generated by hypermail 2b29 : Fri Apr 07 2000 - 21:00:16 EST