Implemented a random number generating function which returns a 6 digit random number
This commit is contained in:
parent
082c2c6a6c
commit
18b602bbca
@ -1,4 +1,6 @@
|
||||
#include "app_utils.h"
|
||||
#include <math.h>
|
||||
#include "wm_crypto_hard.h"
|
||||
|
||||
static uint32_t millis_cnt = 0;
|
||||
|
||||
@ -52,4 +54,23 @@ void ms_delay(uint32_t ms)
|
||||
while(tls_timer_read(timer_id) < ms);
|
||||
|
||||
tls_timer_destroy(timer_id);
|
||||
}
|
||||
|
||||
uint32_t random_gen_6_digit(void)
|
||||
{
|
||||
unsigned char random_buf[6] = {0};
|
||||
uint32_t output_num = 0;
|
||||
|
||||
tls_crypto_random_init(0x19031998, CRYPTO_RNG_SWITCH_16);
|
||||
tls_crypto_random_bytes(random_buf, sizeof random_buf);
|
||||
tls_crypto_random_stop();
|
||||
|
||||
for(uint8_t i = 0; i < sizeof random_buf; i++)
|
||||
{
|
||||
// Ensures the last digit is not 0
|
||||
if(i == (sizeof random_buf) - 1 && random_buf[i] % 10 == 0)random_buf[i]++;
|
||||
output_num += (random_buf[i] % 10) * pow(10, i);
|
||||
}
|
||||
|
||||
return output_num;
|
||||
}
|
@ -11,4 +11,6 @@ void us_delay(uint32_t us);
|
||||
|
||||
void ms_delay(uint32_t ms);
|
||||
|
||||
uint32_t random_gen_6_digit(void);
|
||||
|
||||
#endif //APP_UTILS_H
|
Loading…
Reference in New Issue
Block a user