Gentoo Archives: gentoo-user

From: Matti Nykyri <Matti.Nykyri@×××.fi>
To: gentoo-user@l.g.o
Subject: Re: [gentoo-user] OT: Mapping random numbers (PRNG)
Date: Fri, 06 Jun 2014 21:05:32
Message-Id: 20140606210525.GB1631@lyseo.edu.ouka.fi
In Reply to: Re: [gentoo-user] OT: Mapping random numbers (PRNG) by meino.cramer@gmx.de
1 On Fri, Jun 06, 2014 at 08:39:28PM +0200, meino.cramer@×××.de wrote:
2 > Canek Peláez Valdés <caneko@×××××.com> [14-06-06 17:36]:
3 > > On Thu, Jun 5, 2014 at 9:56 PM, <meino.cramer@×××.de> wrote:
4 > > > Hi,
5 > > >
6 > > > I am experimenting with the C code of the ISAAC pseudo random number generator
7 > > > (http://burtleburtle.net/bob/rand/isaacafa.html).
8 > > >
9 > > > Currently the implementation creates (on my embedded linux) 32 bit
10 > > > hexadecimal output.
11 > >
12 > > So it's a 32 bit integer.
13 > >
14 > > > From this I want to create random numbers in the range of [a-Za-z0-9]
15 > > > *without violating randomness* and (if possible) without throwing
16 > > > away bits of the output.
17 > >
18 > > You mean *characters* int the range [A-Za-z0-9]?
19 > >
20 > > > How can I do this mathemtically (in concern of the quality of output)
21 > > > correct?
22 > >
23 > > The easiest thing to do would be:
24 > >
25 > > -------------------------------------------------------------------------------
26 > > #include <time.h>
27 > > #include <stdio.h>
28 > > #include <stdlib.h>
29 > >
30 > > #define N (26+26+10)
31 > >
32 > > static char S[] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
33 > > 'K', 'L', 'M',
34 > > 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
35 > > 'X', 'Y', 'Z',
36 > > 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
37 > > 'k', 'l', 'm',
38 > > 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
39 > > 'x', 'y', 'z',
40 > > '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
41 > >
42 > > int
43 > > next_character()
44 > > {
45 > > // Use the correct call for ISAAC instead of rand()
46 > > unsigned int idx = rand() % N;
47 > > return S[idx];
48 > > }
49 > >
50 > > int
51 > > main(int argc, char* argv[])
52 > > {
53 > > // Use the correct call for initializing the ISAAC seed
54 > > srand((unsigned int)time(NULL));
55 > > for (int i = 0; i < 20; i++) // --std=c99
56 > > printf("%c\n", next_character());
57 > > return 0;
58 > > }
59 > > -------------------------------------------------------------------------------
60 > >
61 > > If the ISAAC RNG has a good distribution, then the next_character()
62 > > function will give a good distribution among the set [A-Za-z0-9].
63 > >
64 > > Unless I missunderstood what you meant with "create random numbers in
65 > > the range of [a-Za-z0-9]".
66 > >
67 > > Regards.
68 > > --
69 > > Canek Peláez Valdés
70 > > Profesor de asignatura, Facultad de Ciencias
71 > > Universidad Nacional Autónoma de México
72 > >
73 >
74 > Hi,
75 >
76 > Thank you very much for the input! :)
77 >
78 > I have a question about the algorithm:
79 > Suppose rand() has an equal distribution of numbers and furthermore
80 > one has a count of 2^32 random numbers listed in numerical sort
81 > order.
82 > In this list each number would appear (nearly) with the same count: 1
83 >
84 > To get an better imagination of that...suppose the rand() would only
85 > return numbers in the range of 1...12 and the alphabet has only 8
86 > characters (as 2^32 is not devideable by 62)
87 >
88 > rand():
89 > 1 2 3 4 5 6 7 8 9 10 11 12
90 >
91 > rand()%N : rand()%7
92 > 1 2 3 4 5 6 7 0 1 2 3 4
93 >
94 > or in other words: An even distribution of numbers of rand()
95 > would result in a unevenly distributed sequence of characters...or?
96 > This would break the quality of ISAACs output.
97 >
98 > I am sure I did something wrong here...but where is the logic trap?
99 >
100
101 This is the thing I explained in my message.
102
103 --
104 -Matti