Gentoo Archives: gentoo-user

From: "Canek Peláez Valdés" <caneko@×××××.com>
To: gentoo-user@l.g.o
Subject: Re: [gentoo-user] OT: Mapping random numbers (PRNG)
Date: Fri, 06 Jun 2014 03:59:18
Message-Id: CADPrc81Uz0rRcvscWSL-T57RBFHOru-oMrQ94oDaFToLyYrujA@mail.gmail.com
In Reply to: [gentoo-user] OT: Mapping random numbers (PRNG) by meino.cramer@gmx.de
1 On Thu, Jun 5, 2014 at 9:56 PM, <meino.cramer@×××.de> wrote:
2 > Hi,
3 >
4 > I am experimenting with the C code of the ISAAC pseudo random number generator
5 > (http://burtleburtle.net/bob/rand/isaacafa.html).
6 >
7 > Currently the implementation creates (on my embedded linux) 32 bit
8 > hexadecimal output.
9
10 So it's a 32 bit integer.
11
12 > From this I want to create random numbers in the range of [a-Za-z0-9]
13 > *without violating randomness* and (if possible) without throwing
14 > away bits of the output.
15
16 You mean *characters* int the range [A-Za-z0-9]?
17
18 > How can I do this mathemtically (in concern of the quality of output)
19 > correct?
20
21 The easiest thing to do would be:
22
23 -------------------------------------------------------------------------------
24 #include <time.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27
28 #define N (26+26+10)
29
30 static char S[] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
31 'K', 'L', 'M',
32 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
33 'X', 'Y', 'Z',
34 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
35 'k', 'l', 'm',
36 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
37 'x', 'y', 'z',
38 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
39
40 int
41 next_character()
42 {
43 // Use the correct call for ISAAC instead of rand()
44 unsigned int idx = rand() % N;
45 return S[idx];
46 }
47
48 int
49 main(int argc, char* argv[])
50 {
51 // Use the correct call for initializing the ISAAC seed
52 srand((unsigned int)time(NULL));
53 for (int i = 0; i < 20; i++) // --std=c99
54 printf("%c\n", next_character());
55 return 0;
56 }
57 -------------------------------------------------------------------------------
58
59 If the ISAAC RNG has a good distribution, then the next_character()
60 function will give a good distribution among the set [A-Za-z0-9].
61
62 Unless I missunderstood what you meant with "create random numbers in
63 the range of [a-Za-z0-9]".
64
65 Regards.
66 --
67 Canek Peláez Valdés
68 Profesor de asignatura, Facultad de Ciencias
69 Universidad Nacional Autónoma de México

Replies

Subject Author
Re: [gentoo-user] OT: Mapping random numbers (PRNG) meino.cramer@×××.de
Re: [gentoo-user] OT: Mapping random numbers (PRNG) Matti Nykyri <Matti.Nykyri@×××.fi>