<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>8-bit MicrocontrollersのトピックRandom number generation</title>
    <link>https://community.nxp.com/t5/8-bit-Microcontrollers/Random-number-generation/m-p/124658#M387</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hello! I would like to have some examples of random number generation on HCS08 with CodeWarrior, would you please share some ideas? Thanks in advance, Richard&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Thu, 01 Mar 2007 00:37:17 GMT</pubDate>
    <dc:creator>KRichard</dc:creator>
    <dc:date>2007-03-01T00:37:17Z</dc:date>
    <item>
      <title>Random number generation</title>
      <link>https://community.nxp.com/t5/8-bit-Microcontrollers/Random-number-generation/m-p/124658#M387</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hello! I would like to have some examples of random number generation on HCS08 with CodeWarrior, would you please share some ideas? Thanks in advance, Richard&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 01 Mar 2007 00:37:17 GMT</pubDate>
      <guid>https://community.nxp.com/t5/8-bit-Microcontrollers/Random-number-generation/m-p/124658#M387</guid>
      <dc:creator>KRichard</dc:creator>
      <dc:date>2007-03-01T00:37:17Z</dc:date>
    </item>
    <item>
      <title>Re: Random number generation</title>
      <link>https://community.nxp.com/t5/8-bit-Microcontrollers/Random-number-generation/m-p/124659#M388</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;DIV&gt;&lt;FONT size="2"&gt;Hello,&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT size="2"&gt;&lt;/FONT&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT size="2"&gt;What size number do you require?&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT size="2"&gt;&lt;/FONT&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT size="2"&gt;If the random number is required in response to an external event (that may occur at a "random" time), and the events are not closely spaced,&amp;nbsp;it may be possible to utilize the TIM or TPM counter value.&amp;nbsp; For this simple method, the number should probably not exceed byte size, and I would probably use the LS byte of the timer, and swap the high and low nybbles to further "randomize" the value.&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT size="2"&gt;&lt;/FONT&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT size="2"&gt;However, if you need a true "quasi-random" value, you might be able to easily&amp;nbsp;adapt an existing&amp;nbsp;function that calculates&amp;nbsp;CRC values of the size you need.&amp;nbsp; I recall that there are some&amp;nbsp;recent threads on this subject&amp;nbsp;in these forums, so use the search engine.&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT size="2"&gt;&lt;/FONT&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT size="2"&gt;You could use the timer value as the initial "seed", and calculate the first CRC value based on this. The next CRC calculation would then use the previous CRC value, and so on.&amp;nbsp; The CRC polynomial used would not matter for this application.&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT size="2"&gt;&lt;/FONT&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT size="2"&gt;Regards,&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT size="2"&gt;Mac&lt;/FONT&gt;&lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 01 Mar 2007 01:24:15 GMT</pubDate>
      <guid>https://community.nxp.com/t5/8-bit-Microcontrollers/Random-number-generation/m-p/124659#M388</guid>
      <dc:creator>bigmac</dc:creator>
      <dc:date>2007-03-01T01:24:15Z</dc:date>
    </item>
    <item>
      <title>Re: Random number generation</title>
      <link>https://community.nxp.com/t5/8-bit-Microcontrollers/Random-number-generation/m-p/124660#M389</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;DIV&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT size="2"&gt;Hello,&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT size="2"&gt;&lt;/FONT&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT size="2"&gt;To further clarify the possible use of a CRC algorithm to generate a quasi-random value, I found the following code to generate an 8-bit CRC value.&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT size="2"&gt;&lt;/FONT&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT face="Courier New" size="2"&gt;/* Update 8-bit CRC value&lt;BR /&gt;&amp;nbsp;&amp;nbsp;using polynomial&amp;nbsp; X^8 + X^5 + X^4 + 1 */&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT face="Courier New" size="2"&gt;&lt;/FONT&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT face="Courier New" size="2"&gt;#define POLYVAL 0x8C&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT face="Courier New" size="2"&gt;&lt;/FONT&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT face="Courier New" size="2"&gt;void update_crc(unsigned char new, unsigned char *crc)&lt;BR /&gt;{&lt;BR /&gt;&amp;nbsp;&amp;nbsp;unsigned char c, i;&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT face="Courier New" size="2"&gt;&lt;/FONT&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT size="2"&gt;&lt;FONT face="Courier New"&gt;&amp;nbsp;&amp;nbsp;c = *crc;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;for (i = 0; i &amp;lt; 8; i++) {&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if ((c ^ new) &amp;amp; 1) c = (c &amp;gt;&amp;gt; 1 ) ^ POLYVAL;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;else c &amp;gt;&amp;gt;= 1;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;new &amp;gt;&amp;gt;= 1;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;}&lt;BR /&gt;&amp;nbsp;&amp;nbsp;*crc = c;&lt;BR /&gt;}&lt;BR /&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT size="2"&gt;&lt;/FONT&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT size="2"&gt;To use this function to generate a quasi-random value, I think the following would work -&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT size="2"&gt;Firstlly, define and initialise a global variable to contain the random value&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT size="2"&gt;&lt;/FONT&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT face="Courier New" size="2"&gt;unsigned char&amp;nbsp;rndval = SEED;&amp;nbsp; // Must be non-zero value&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT size="2"&gt;&lt;/FONT&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT size="2"&gt;Then when the next random value is required, and &lt;FONT face="Courier New"&gt;rndval&lt;/FONT&gt; is to be updated, use the following code -&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT size="2"&gt;&lt;/FONT&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT face="Courier New" size="2"&gt;&amp;nbsp;&amp;nbsp;update_crc(0xFF, &amp;amp;rndval);&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT size="2"&gt;&lt;/FONT&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT size="2"&gt;My understanding is that the first parameter could actually be any non-zero constant.&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT size="2"&gt;&lt;/FONT&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT size="2"&gt;Regards,&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT size="2"&gt;Mac&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT size="2"&gt;&lt;/FONT&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT size="2"&gt;&lt;/FONT&gt;&amp;nbsp;&lt;/DIV&gt;&lt;P&gt;Message Edited by bigmac on &lt;SPAN class="date_text"&gt;2007-03-01&lt;/SPAN&gt;&lt;SPAN class="time_text"&gt;12:49 PM&lt;/SPAN&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 01 Mar 2007 10:46:54 GMT</pubDate>
      <guid>https://community.nxp.com/t5/8-bit-Microcontrollers/Random-number-generation/m-p/124660#M389</guid>
      <dc:creator>bigmac</dc:creator>
      <dc:date>2007-03-01T10:46:54Z</dc:date>
    </item>
    <item>
      <title>Re: Random number generation</title>
      <link>https://community.nxp.com/t5/8-bit-Microcontrollers/Random-number-generation/m-p/124661#M390</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;DIV&gt;Whatever method you use to generate the number, I would reseed it every time it's needed, and use a seed tied to a non-synchronous event to the code.&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;Obviously it's impossible to truely random numbers, but if the call to generate a number does the seed based on when it's called in the program against say a shuffle of a timer counter or something, then you at least won't get the same number after a reboot and executing the exact same sequence every time.&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 01 Mar 2007 22:23:27 GMT</pubDate>
      <guid>https://community.nxp.com/t5/8-bit-Microcontrollers/Random-number-generation/m-p/124661#M390</guid>
      <dc:creator>mke_et</dc:creator>
      <dc:date>2007-03-01T22:23:27Z</dc:date>
    </item>
    <item>
      <title>Re: Random number generation</title>
      <link>https://community.nxp.com/t5/8-bit-Microcontrollers/Random-number-generation/m-p/124662#M391</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;DIV&gt;I try to stay away from values based only on timers and functions (CRC) because they tend to give the same result each time unless the request is generated by a human.&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;If you have a fairly noisy A/D input you could also XOR that in with your timer based value to help randomize it. I've even heard of people leaving unused A/D inputs floating to pick up radio noise for random number generation.&lt;/DIV&gt;&lt;DIV&gt;In one system, I took the results from all 8 A/D inputs and XOR'd them together as a seed value then I XOR'd that with a timer and the previous random value&amp;nbsp;and put it through a scrambling function like the CRC. Pretty random!&lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 03 Mar 2007 06:30:55 GMT</pubDate>
      <guid>https://community.nxp.com/t5/8-bit-Microcontrollers/Random-number-generation/m-p/124662#M391</guid>
      <dc:creator>dwhite</dc:creator>
      <dc:date>2007-03-03T06:30:55Z</dc:date>
    </item>
    <item>
      <title>Re: Random number generation - generating the seeds</title>
      <link>https://community.nxp.com/t5/8-bit-Microcontrollers/Random-number-generation/m-p/124663#M392</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;In years past, I have used two methods to generate seeds for pseudo-random number generators.&lt;BR /&gt;&lt;BR /&gt;In one, I had a very fast free-running counter that I would read and reset every time a button was pressed on the user interface, generating a fresh seed every button-press. When I needed a random number, I would generate it from the new seed if it was available. Otherwise, I would generate it from the previous random number if a fresh seed was not available yet.&lt;BR /&gt;&lt;BR /&gt;In a similar vane to what dwhite mentioned, I once needed a 16-bit analog-to-digital converter tied to the AC line for calculating power. When I needed a new seed, I grabbed the most recent value returned by the AtoD. But in that case, the need for a random number was rare, and completely asynchronous with regard to the AC line.&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 03 Mar 2007 09:44:12 GMT</pubDate>
      <guid>https://community.nxp.com/t5/8-bit-Microcontrollers/Random-number-generation/m-p/124663#M392</guid>
      <dc:creator>rocco</dc:creator>
      <dc:date>2007-03-03T09:44:12Z</dc:date>
    </item>
    <item>
      <title>Re: Random number generation</title>
      <link>https://community.nxp.com/t5/8-bit-Microcontrollers/Random-number-generation/m-p/124664#M393</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;First you need to decide whether "unpredictability" is important or not, especially compared to the sorts of statistical properties that random numbers are supposed to generate for simulations and such. Unpredictability is extremely important for cryptographic sorts of applications, but not so much for anything else (and of course it makes debugging harder.) The pseudo-random-number generators based on CRC-like calculations are adequate for many applications (though I'd beware of doing common&lt;BR /&gt;operations like "modulo" to get smaller random numbers if statistical properties are&lt;BR /&gt;important; you might interfere with the "randomness"...)&lt;BR /&gt;&lt;BR /&gt;Getting "real", unpredictable random numbers in a deterministic system like a microcontroller is pretty tough. You can pick up a couple bits worth of randomness by timing external events like keypresses with a high-speed timer, but...&lt;BR /&gt;&lt;BR /&gt;I keep expecting the next generation of micros to have built-in hardware support for generating random numbers (multiple noisy semiconductor junctions driving multiple counters, or something.) But I don't know if the people who want such things would consider them 'trustworthy' or not...&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 03 Mar 2007 10:04:58 GMT</pubDate>
      <guid>https://community.nxp.com/t5/8-bit-Microcontrollers/Random-number-generation/m-p/124664#M393</guid>
      <dc:creator>billw</dc:creator>
      <dc:date>2007-03-03T10:04:58Z</dc:date>
    </item>
  </channel>
</rss>

