From: Marc Joliet <marcec@gmx.de>
To: gentoo-user@lists.gentoo.org
Subject: Re: [gentoo-user] OT: Mapping random numbers (PRNG)
Date: Sat, 7 Jun 2014 14:13:11 +0200 [thread overview]
Message-ID: <20140607141311.0baaa29e@marcec> (raw)
In-Reply-To: <20140607091911.GA24542@lyseo.edu.ouka.fi>
[-- Attachment #1: Type: text/plain, Size: 1404 bytes --]
Am Sat, 7 Jun 2014 12:19:11 +0300
schrieb Matti Nykyri <Matti.Nykyri@iki.fi>:
> On Sat, Jun 07, 2014 at 12:03:29AM +0300, Matti Nykyri wrote:
[...]
> unsigned char get_6bits ()
> {
> static unsigned int rand = 0; //(sizeof(int) = 32)
Just an itty bitty nitpick: since you already require C99 as per the for loop
below, you might as well use uint32_t (from stdint.h) instead of assuming that
sizeof(int) == 32 :) .
> static char bits_avail = 0;
> unsigned char result = 0;
>
> //get 2 bits 3 times: 32 is devidable by 2
> for (int i = 0; i < 3; i++) { // --std=c99
> //Fill buffer if it is empty!
> if (!bits_avail || bits_avail < 0 ) { //if bits_avail < 0 it is an error!
> // Use the correct call for ISAAC instead of rand()
> rand = rand();
>
> bits_avail = 32;
> }
>
> result <<= 2; //move two bits to left.
> result = result | (rand & 0x3); //add two least signifigant bits to the result.
> rand >>= 2; //move two bits to right.
> bits_avail -= 2;
> }
>
> return result; //result has 6 bits of random data...
> }
>
> char next_character()
> {
> unsigned char idx = 0;
> do {
> idx = get_6bits();
> } while (idx > 61);
>
> return S[idx];
> }
>
> Very simple :)
[...]
--
Marc Joliet
--
"People who think they know everything really annoy those of us who know we
don't" - Bjarne Stroustrup
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 836 bytes --]
next prev parent reply other threads:[~2014-06-07 12:13 UTC|newest]
Thread overview: 26+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-06-06 2:56 [gentoo-user] OT: Mapping random numbers (PRNG) meino.cramer
2014-06-06 3:58 ` Canek Peláez Valdés
2014-06-06 18:39 ` meino.cramer
2014-06-06 19:37 ` null_ptr
2014-06-06 19:55 ` Canek Peláez Valdés
2014-06-06 21:05 ` Matti Nykyri
2014-06-06 21:03 ` Matti Nykyri
2014-06-07 9:19 ` Matti Nykyri
2014-06-07 12:13 ` Marc Joliet [this message]
2014-06-26 21:00 ` [gentoo-user] " Kai Krakow
2014-06-26 22:07 ` Kai Krakow
2014-06-27 8:55 ` thegeezer
2014-06-27 17:49 ` Matti Nykyri
2014-06-27 17:50 ` [gentoo-user] " Kai Krakow
2014-06-27 20:59 ` Neil Bothwick
2014-06-27 21:13 ` [gentoo-user] " Matti Nykyri
2014-06-28 13:08 ` Matti Nykyri
2014-06-28 21:28 ` [gentoo-user] " Kai Krakow
2014-06-28 22:06 ` Canek Peláez Valdés
2014-06-29 0:37 ` gottlieb
2014-06-29 0:53 ` Canek Peláez Valdés
2014-06-29 1:46 ` [gentoo-user] " »Q«
2014-06-29 2:57 ` Canek Peláez Valdés
2014-06-29 6:24 ` [gentoo-user] " Matti Nykyri
2014-06-29 12:38 ` [gentoo-user] " Kai Krakow
2014-06-30 23:47 ` Matti Nykyri
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20140607141311.0baaa29e@marcec \
--to=marcec@gmx.de \
--cc=gentoo-user@lists.gentoo.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox