<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Random number generation on 8-bit AVR</title>
	<atom:link href="http://www.reenigne.org/blog/random-number-generation-on-8-bit-avr/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.reenigne.org/blog/random-number-generation-on-8-bit-avr/</link>
	<description>Stuff I think about</description>
	<lastBuildDate>Sun, 22 Jan 2012 21:39:31 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
	<item>
		<title>By: Andrew</title>
		<link>http://www.reenigne.org/blog/random-number-generation-on-8-bit-avr/comment-page-1/#comment-3999</link>
		<dc:creator>Andrew</dc:creator>
		<pubDate>Fri, 25 Feb 2011 03:36:58 +0000</pubDate>
		<guid isPermaLink="false">http://www.reenigne.org/blog/?p=1159#comment-3999</guid>
		<description>Yes, you can do it that way too but an 8x8 bit multiply is only 2 cycles on 8-bit AVR so there isn&#039;t so much advantage to avoiding LCGs. You also still need to do the &quot;mod N&quot; opreation. Also, a routine with 8 bits of state would not have been enough for this application - it would have repeated too quickly and lower-numbered LEDs would have shown up significantly more often than lower-numbered ones.</description>
		<content:encoded><![CDATA[<p>Yes, you can do it that way too but an 8x8 bit multiply is only 2 cycles on 8-bit AVR so there isn't so much advantage to avoiding LCGs. You also still need to do the "mod N" opreation. Also, a routine with 8 bits of state would not have been enough for this application - it would have repeated too quickly and lower-numbered LEDs would have shown up significantly more often than lower-numbered ones.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Richard B. Johnson</title>
		<link>http://www.reenigne.org/blog/random-number-generation-on-8-bit-avr/comment-page-1/#comment-3998</link>
		<dc:creator>Richard B. Johnson</dc:creator>
		<pubDate>Fri, 25 Feb 2011 01:27:49 +0000</pubDate>
		<guid isPermaLink="false">http://www.reenigne.org/blog/?p=1159#comment-3998</guid>
		<description>Simple 8-bit RNG in &#039;C&#039;

//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
//
//    8-bit random number generator.
//
//    Copyright 2011  Richard B. Johnson
//    rjohnson@Route495Software.com
//
//    You can use this if you preserve the Copyright notice above.
//
#include 

#if !defined(uint8_t)
typedef unsigned char uint8_t;
#endif

static uint8_t Seed = 0x00;
static uint8_t Magic = 0x5c;

//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
//
//   This, using the &#039;C&#039; language, rotates all the bits in val to the right
//   the number of bits in nr.
//

static uint8_t RotateRight(uint8_t val, int nr)
{
    uint8_t tmp;
    while(nr--)
    {
        tmp = val &amp; 1;
        tmp &lt;&gt;= 1;
        val &#124;= tmp;
    }
    return val;
}
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
//
//  Ths pseudo random number generator.
//
uint8_t Rand()
{
    Seed = RotateRight(Seed, 3);
    Seed += Magic;
    
    return Seed;
}
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
//
//   Seed the random number generator.
//
void Srand(uint8_t val)
{
    Seed = val;
}
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
//
//    This is for testing. Define SETUP to find tha correct magic number
//    to hard-code.
//

#define TESTING

#ifdef TESTING


#define NoSETUP

int main()
{
    int i;
#ifdef SETUP
    for(;;)
    {
        Seed = 0;
        printf(&quot;%02x\n&quot;, Magic);
        i = 0;
        while(Rand())
            i++;
        if(i &lt; 0xfe)
            Magic++;
        else
            break;
    }
#else
    for(i=0; i&lt; 0xff; i++)
        printf(&quot;%02x\n&quot;, Rand());

#endif

    return 0;

}

#endif
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-</description>
		<content:encoded><![CDATA[<p>Simple 8-bit RNG in 'C'</p>
<p>//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-<br />
//<br />
//    8-bit random number generator.<br />
//<br />
//    Copyright 2011  Richard B. Johnson<br />
//    <a href="mailto:rjohnson@Route495Software.com">rjohnson@Route495Software.com</a><br />
//<br />
//    You can use this if you preserve the Copyright notice above.<br />
//<br />
#include </p>
<p>#if !defined(uint8_t)<br />
typedef unsigned char uint8_t;<br />
#endif</p>
<p>static uint8_t Seed = 0x00;<br />
static uint8_t Magic = 0x5c;</p>
<p>//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-<br />
//<br />
//   This, using the 'C' language, rotates all the bits in val to the right<br />
//   the number of bits in nr.<br />
//</p>
<p>static uint8_t RotateRight(uint8_t val, int nr)<br />
{<br />
    uint8_t tmp;<br />
    while(nr--)<br />
    {<br />
        tmp = val &amp; 1;<br />
        tmp &lt;&gt;= 1;<br />
        val |= tmp;<br />
    }<br />
    return val;<br />
}<br />
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-<br />
//<br />
//  Ths pseudo random number generator.<br />
//<br />
uint8_t Rand()<br />
{<br />
    Seed = RotateRight(Seed, 3);<br />
    Seed += Magic;</p>
<p>    return Seed;<br />
}<br />
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-<br />
//<br />
//   Seed the random number generator.<br />
//<br />
void Srand(uint8_t val)<br />
{<br />
    Seed = val;<br />
}<br />
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-<br />
//<br />
//    This is for testing. Define SETUP to find tha correct magic number<br />
//    to hard-code.<br />
//</p>
<p>#define TESTING</p>
<p>#ifdef TESTING</p>
<p>#define NoSETUP</p>
<p>int main()<br />
{<br />
    int i;<br />
#ifdef SETUP<br />
    for(;;)<br />
    {<br />
        Seed = 0;<br />
        printf("%02x\n", Magic);<br />
        i = 0;<br />
        while(Rand())<br />
            i++;<br />
        if(i &lt; 0xfe)<br />
            Magic++;<br />
        else<br />
            break;<br />
    }<br />
#else<br />
    for(i=0; i&lt; 0xff; i++)<br />
        printf(&quot;%02x\n&quot;, Rand());</p>
<p>#endif</p>
<p>    return 0;</p>
<p>}</p>
<p>#endif<br />
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-<br />
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-<br />
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-</p>
]]></content:encoded>
	</item>
</channel>
</rss>

