Gentoo Archives: gentoo-user

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

Replies

Subject Author
Re: [gentoo-user] OT: Mapping random numbers (PRNG) null_ptr <rhannek@×××.de>
Re: [gentoo-user] OT: Mapping random numbers (PRNG) "Canek Peláez Valdés" <caneko@×××××.com>
Re: [gentoo-user] OT: Mapping random numbers (PRNG) Matti Nykyri <Matti.Nykyri@×××.fi>