dimanche 3 avril 2022

How to generate a random number between min and max in linux KERNEL

I need to get a number in a certain range in linux kernel. Min number 30 max 5000. It should not be a one-time function.

I have working code which works in linux(not kernel):

#include <stdlib.h>
#define max_ppm 5000
#define min_ppm 30
int get_ppm_value()
{
   srand(time(NULL));
   int ppm_value;
   ppm_value= (rand() % (max_ppm + 1 - min_ppm)) + min_ppm; 
   return ppm_value;
}

This works great and every time gives me a new desired number.

Question:

  1. The first question is theoretical why can't I use the C libraries in the kernel (stdlib). In addition to the answer, the speed of execution and the amount of code.
  2. How can I do it better in the kernel? using the get_random_bytes() function (use it also i.e.(get_random_bytes() % (max_ppm + 1 - min_ppm)) + min_ppm)) or from /dev/random or /dev/urandom
  3. is there an alternative to this library #include <stdlib.h>

This is very serious for me, so I ask you to also treat my question as well.




Aucun commentaire:

Enregistrer un commentaire