SolarEnergy/esp32_device/main/random.c

31 lines
538 B
C
Raw Normal View History

2024-07-27 13:31:17 +00:00
#include "random.h"
#include "esp_random.h"
void rand_str(size_t len, char *dest)
{
for (size_t i = 0; i < len; i++)
{
uint32_t v = esp_random() % (26 * 2 + 10);
// Upper case letter
if (v < 26)
{
dest[i] = 65 + v;
continue;
}
v -= 26;
// Lower case letter
if (v < 26)
{
dest[i] = 97 + v;
continue;
}
v -= 26;
// Digit
dest[i] = 48 + (v % 10);
}
dest[len] = '\0';
}