This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

issue with NRF RNG generator

hi,

I am working with nr52832 and SDK 12.1. I have referred the RNG example based on which I have integrated that code section in my custom application:

     rng_generate(rand_arr1, RAND_STREAM_BYTES); //example output: 15234
     rng_generate(rand_arr2, RAND_STREAM_BYTES); //15234

    ... user generated delay

    rng_generate(rand_arr1, RAND_STREAM_BYTES); //23145
    rng_generate(rand_arr2, RAND_STREAM_BYTES); //23145

and the code for rng_generate is:

void rng_generate(uint8_t *rand_arr, uint8_t len)
{
  //nrf_drv_rng_config_t rand_config;
  uint8_t bytes_avail;
  uint32_t err_code;
  err_code = nrf_drv_rng_bytes_available(&bytes_avail);
  APP_ERROR_CHECK(err_code);
  uint8_t length = (len<bytes_avail) ? len : bytes_avail;
  err_code = nrf_drv_rng_rand(rand_arr,length);
  APP_ERROR_CHECK(err_code);

}

I have initialised rng in main. My issue is: When I call the rng_generate twice immediately, I get the same number in rand_arr1 and rnad_arr2. This number changes after some user-generated delay is included (which is huge). But again if there are two immediate calls to rng_generate, I get the same number. I tried to include a few ms delay between them, but the results were same. How can I avoid this issue? Or is than any alternate api for generating true non-deterministic random numbers?

Thanks

Parents Reply
  • It might be that the UART communication is blocking the device from performing the random number generation. You should use the softdevice functions when a softdevice is active. This is just a safe API to avoid timing issues in the softdevice, it still use the hardware RNG module. I'm not aware of any configurations to set limits on the generated numbers. The RNG module output a given number of 1 byte random values. To get your desired limits, you can generate two bytes, merge it into a 16-bit unsigned integer (max 65535), and discard the ones below 10000:

    uint8_t p_buff[2];
    uint16_t combined = 0;
    do
    {
        uint8_t length = random_vector_generate(p_buff,2);
        combined = (p_buff[0] << 8) | (p_buff[1] & 0xFF);
     }
     while (combined < 10000);
        
     NRF_LOG_INFO("Random Vector: %d\r\n",combined);
    
Children
No Data
Related