Gentoo Archives: gentoo-user

From: Marc Joliet <marcec@×××.de>
To: gentoo-user@l.g.o
Subject: Re: [gentoo-user] OT: Mapping random numbers (PRNG)
Date: Sat, 07 Jun 2014 12:13:24
Message-Id: 20140607141311.0baaa29e@marcec
In Reply to: Re: [gentoo-user] OT: Mapping random numbers (PRNG) by Matti Nykyri
1 Am Sat, 7 Jun 2014 12:19:11 +0300
2 schrieb Matti Nykyri <Matti.Nykyri@×××.fi>:
3
4 > On Sat, Jun 07, 2014 at 12:03:29AM +0300, Matti Nykyri wrote:
5 [...]
6 > unsigned char get_6bits ()
7 > {
8 > static unsigned int rand = 0; //(sizeof(int) = 32)
9
10 Just an itty bitty nitpick: since you already require C99 as per the for loop
11 below, you might as well use uint32_t (from stdint.h) instead of assuming that
12 sizeof(int) == 32 :) .
13
14 > static char bits_avail = 0;
15 > unsigned char result = 0;
16 >
17 > //get 2 bits 3 times: 32 is devidable by 2
18 > for (int i = 0; i < 3; i++) { // --std=c99
19 > //Fill buffer if it is empty!
20 > if (!bits_avail || bits_avail < 0 ) { //if bits_avail < 0 it is an error!
21 > // Use the correct call for ISAAC instead of rand()
22 > rand = rand();
23 >
24 > bits_avail = 32;
25 > }
26 >
27 > result <<= 2; //move two bits to left.
28 > result = result | (rand & 0x3); //add two least signifigant bits to the result.
29 > rand >>= 2; //move two bits to right.
30 > bits_avail -= 2;
31 > }
32 >
33 > return result; //result has 6 bits of random data...
34 > }
35 >
36 > char next_character()
37 > {
38 > unsigned char idx = 0;
39 > do {
40 > idx = get_6bits();
41 > } while (idx > 61);
42 >
43 > return S[idx];
44 > }
45 >
46 > Very simple :)
47 [...]
48
49 --
50 Marc Joliet
51 --
52 "People who think they know everything really annoy those of us who know we
53 don't" - Bjarne Stroustrup

Attachments

File name MIME type
signature.asc application/pgp-signature