<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://devzone.nordicsemi.com/cfs-file/__key/system/syndication/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Random number generator not so random</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/64358/random-number-generator-not-so-random</link><description>Hello DevZone, 
 
 SDK 15.0.0, sd132 
 I&amp;#39;m working on the random number generator peripheral in the nRF52832 and used the peripheral example in my SDK. 
 When I upload the example code to my devkit I can clearly see that every time the microcontroller</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Tue, 12 Mar 2024 16:53:35 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/64358/random-number-generator-not-so-random" /><item><title>RE: Random number generator not so random</title><link>https://devzone.nordicsemi.com/thread/473507?ContentTypeID=1</link><pubDate>Tue, 12 Mar 2024 16:53:35 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:7a7dd108-7ecd-47b0-bd8a-5114ac6eac2e</guid><dc:creator>Toastus</dc:creator><description>&lt;p&gt;I know this is an old question, but I see no updates to similar questions so I thought I&amp;#39;d point it out. There are two bugs in this snippet.&lt;/p&gt;
&lt;p&gt;1) uReady cannot be used in the while loop. It won&amp;#39;t work correctly. You must check the NRF_RNG-&amp;gt;EVENTS_VALRDY in the loop itself. uReady won&amp;#39;t be magically updated inside the loop, just set to whatever value NRF_RNG-&amp;gt;EVENTS_VALRDY was set to prior to running the loop. This must be fixed before bug #2.&lt;/p&gt;
&lt;p&gt;2) NRF_RNG-&amp;gt;EVENTS_VALRDY needs to be set to 0 by you prior to entering the loop. Chances are, if other tasks are using it or have used it, you&amp;#39;ll find the value is 1 all the time and the task will return the same random number every time.&lt;/p&gt;
&lt;p&gt;This isn&amp;#39;t a bug in NRF_RNG, it&amp;#39;s the code itself. You mentioned you copied it from some example. If it was actually the same line for line, then the example is incorrect also. You will find after fixing this that NRF_RNG will work for you regardless of whether a Softdevice is using it or not.&lt;/p&gt;
&lt;p&gt;The advantage of NRF_RNG is that it is a random number based on hardware, rather than a software psuedorandom number. This is important for secure code implementations.&lt;/p&gt;
&lt;p&gt;(Additionally, the claimed working code above is incorrect as well--it has the same uReady issue and will only work if some other module is resetting the EVENTS_VALRDY independently and you happened to sync up with it in a race condition)&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Hope this helps.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Random number generator not so random</title><link>https://devzone.nordicsemi.com/thread/262532?ContentTypeID=1</link><pubDate>Fri, 31 Jul 2020 09:18:09 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:d2039fac-01d7-46b5-9011-0aff95367ce7</guid><dc:creator>T IJ</dc:creator><description>&lt;p&gt;Yea you&amp;#39;re probably right, I&amp;#39;ve found this that works with the softdevice.&lt;br /&gt;The only thing I dislike about this is waiting for the softdevice to generate a number, but its something I have to deal with.&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;for(uint8_t i = 0; i &amp;lt; 6; i++)
{
    while(len == 0)
    {
        APP_ERROR_CHECK(sd_rand_application_bytes_available_get(&amp;amp;len));
    }
    APP_ERROR_CHECK(sd_rand_application_vector_get(&amp;amp;p_buff[i], len));
    len = 0;
}&lt;/pre&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Random number generator not so random</title><link>https://devzone.nordicsemi.com/thread/262503?ContentTypeID=1</link><pubDate>Fri, 31 Jul 2020 07:16:52 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:dcb485c2-9f8e-4c68-9914-fed263226c95</guid><dc:creator>snoopy20</dc:creator><description>&lt;p&gt;That&amp;#39;s what I&amp;#39;d do. You can probably call the softdevice to try and share the generation but doing it before init is cleaner imo. It comes down to whether you need new numbers later on.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Random number generator not so random</title><link>https://devzone.nordicsemi.com/thread/262499?ContentTypeID=1</link><pubDate>Fri, 31 Jul 2020 06:39:23 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:cb30c842-b942-4d42-a195-baeba7462f54</guid><dc:creator>T IJ</dc:creator><description>&lt;p&gt;Well thanks for your help at least, I&amp;#39;ve figured out that the softdevice indeed uses this peripheral and makes it unusable for me. I think I&amp;#39;ll do all my random number generation prior to starting the softdevice&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Random number generator not so random</title><link>https://devzone.nordicsemi.com/thread/262456?ContentTypeID=1</link><pubDate>Thu, 30 Jul 2020 15:03:24 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:8e337041-ce26-400c-b876-84cd48993642</guid><dc:creator>snoopy20</dc:creator><description>&lt;p&gt;I haven&amp;#39;t used this peripheral but I prefer writing home-brew rather than the abstracted libraries in most cases as you can tell what&amp;#39;s going on. Some things come to mind, the obvious one is the library probably uses interrupts and you are not. Second is the RND is probably used by SoftDevice (I guess) for the encrypted connection so maybe the nrf library is calling it and you have a problem there.&lt;br /&gt;&lt;br /&gt;If you&amp;#39;re doing this on boot before softdevice init and without need to refresh the numbers I&amp;#39;d be happy with your approach, with a loop for however many you need.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Random number generator not so random</title><link>https://devzone.nordicsemi.com/thread/262446?ContentTypeID=1</link><pubDate>Thu, 30 Jul 2020 14:25:46 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:4c31ad82-355f-4a3d-a125-276632d42544</guid><dc:creator>T IJ</dc:creator><description>&lt;p&gt;If I use the following own witten code, the numbers are random every time.&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;NRF_RNG-&amp;gt;CONFIG = RNG_CONFIG_DERCEN_Msk;

NRF_RNG-&amp;gt;TASKS_START = 1;

uint8_t uReady = NRF_RNG-&amp;gt;EVENTS_VALRDY;
while(uReady == 0)
{
    uReady = NRF_RNG-&amp;gt;EVENTS_VALRDY;
}

NRF_RNG-&amp;gt;TASKS_STOP = 1;

NRF_LOG_INFO(&amp;quot;%d&amp;quot;,NRF_RNG-&amp;gt;VALUE);&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Random number generator not so random</title><link>https://devzone.nordicsemi.com/thread/262400?ContentTypeID=1</link><pubDate>Thu, 30 Jul 2020 12:28:16 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:62331b45-9f83-4bb5-be3b-52f1bba2c6ff</guid><dc:creator>T IJ</dc:creator><description>&lt;p&gt;Yes&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Random number generator not so random</title><link>https://devzone.nordicsemi.com/thread/262392?ContentTypeID=1</link><pubDate>Thu, 30 Jul 2020 12:09:07 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:575c9ecc-9c10-4e7a-bca0-019b76fe3301</guid><dc:creator>snoopy20</dc:creator><description>&lt;p&gt;OK. So this is ONE nRF52-DK you are using AND you receive SIX identical numbers. YES/NO?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Random number generator not so random</title><link>https://devzone.nordicsemi.com/thread/262390?ContentTypeID=1</link><pubDate>Thu, 30 Jul 2020 12:05:43 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:fc39570a-d75a-40eb-bb8a-6767cf6e1fd7</guid><dc:creator>T IJ</dc:creator><description>&lt;p&gt;After roughly resetting the device 20 times.&lt;/p&gt;
&lt;p&gt;I&amp;#39;ve added my code below, it is an exact copy from the example except that I only need 6 random numbers instead of the 16 in the example.&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;uint8_t                 available;
uint8_t                 p_buff[6];
nrf_drv_rng_config_t    config;

config.error_correction = 1;
config.interrupt_priority = 7;

err_code = nrf_drv_rng_init(&amp;amp;config);
APP_ERROR_CHECK(err_code);

nrf_drv_rng_bytes_available(&amp;amp;available);
uint8_t length = MIN(6, available);

err_code = nrf_drv_rng_rand(p_buff, length);
APP_ERROR_CHECK(err_code);

&lt;/pre&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Random number generator not so random</title><link>https://devzone.nordicsemi.com/thread/262389?ContentTypeID=1</link><pubDate>Thu, 30 Jul 2020 11:58:51 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:3c8a1dbe-2f01-43f3-a44a-2a8fcbaf5dcd</guid><dc:creator>snoopy20</dc:creator><description>&lt;p&gt;Right, so you are saying that when running the code on multiple nRF52-DK&amp;#39;s you get the same number.&lt;/p&gt;
&lt;p&gt;So when do you not get the same number (first line of original paragraph)?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Random number generator not so random</title><link>https://devzone.nordicsemi.com/thread/262387?ContentTypeID=1</link><pubDate>Thu, 30 Jul 2020 11:53:58 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:0b834c66-0f2f-46dc-9089-cfd185ce6437</guid><dc:creator>T IJ</dc:creator><description>&lt;p&gt;I have multiple standard PCA10040 devkits&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Random number generator not so random</title><link>https://devzone.nordicsemi.com/thread/262385?ContentTypeID=1</link><pubDate>Thu, 30 Jul 2020 11:52:12 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:24979cf0-5454-433d-82e4-f8599fb30f0c</guid><dc:creator>snoopy20</dc:creator><description>&lt;p&gt;First you&amp;#39;re not explaining yourself correctly. You state when you upload to &amp;#39;my devkit&amp;#39; you get a random number, then you later say you don;t get random across &amp;#39;multiple devkits&amp;#39;. Try explaining what a devkit is, what your &amp;#39;project&amp;#39; is (custom prototype board?)...&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>