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

How to use sd_rand_application_vector_get correctly?

  • By default, is it enabling or disabling bias correction? If no, how to enable bias correction?
  • Is the RNG peripheral being left on all the time as a result of us using it, and if so, what is the additional power usage (if any)? Or is it always on already?
  • If the BT stack is also using the generator (I assume it does), is it possible that we could get spurious "NRF_ERROR_SOC_RAND_NOT_ENOUGH_VALUE" errors. What is the proper way to handle this error? wait for a while and then retry? or any recommendation.

Here is our pseudo code and we'd like the value uniformly distributed within [0, RANGE). Is the implementation correct?

      uint16_t random = 0;
      sd_rand_application_vector_get((uint8_t*)&random, sizeof(random)));

      uint16_t value = (uint16_t)((float)random / UINT16_MAX * RANGE);

Parents
  • Hi,

    By default, is it enabling or disabling bias correction? If no, how to enable bias correction?

    Bias correction is enabled.

    Is the RNG peripheral being left on all the time as a result of us using it

    No.

    If the BT stack is also using the generator (I assume it does), is it possible that we could get spurious "NRF_ERROR_SOC_RAND_NOT_ENOUGH_VALUE" errors. What is the proper way to handle this error? wait for a while and then retry? or any recommendation.

    Wait a couple of milliseconds, and call sd_rand_application_bytes_available_get(..) again until you have available bytes in the pool.

    Note that if you call the function very close to the time you enable the SoftDevice, then the SoftDevice might not had enough time to fill the buffer/pool of random numbers. It takes some time after the SoftDevice is enabled to the buffer with random numbers is filled.

    Is the implementation correct?

    Yes, that should work.

Reply
  • Hi,

    By default, is it enabling or disabling bias correction? If no, how to enable bias correction?

    Bias correction is enabled.

    Is the RNG peripheral being left on all the time as a result of us using it

    No.

    If the BT stack is also using the generator (I assume it does), is it possible that we could get spurious "NRF_ERROR_SOC_RAND_NOT_ENOUGH_VALUE" errors. What is the proper way to handle this error? wait for a while and then retry? or any recommendation.

    Wait a couple of milliseconds, and call sd_rand_application_bytes_available_get(..) again until you have available bytes in the pool.

    Note that if you call the function very close to the time you enable the SoftDevice, then the SoftDevice might not had enough time to fill the buffer/pool of random numbers. It takes some time after the SoftDevice is enabled to the buffer with random numbers is filled.

    Is the implementation correct?

    Yes, that should work.

Children
Related