ch32hack/rand.h

49 lines
1.2 KiB
C

#pragma once
#include <stdint.h>
/**
* @brief Sets the seed for the PRNG.
*
* @param s The specific seed value or zero. If zero is passed, it will call
* rand_reseed().
*/
void sprand(uint64_t s);
/**
* @brief Generates a pseudo-random 64-bit number.
*
* Saves PRNG state to flash periodically.
*
* Uses a simple Linear Congruential Generator (LCG) to produce
* a sequence of pseudo-random numbers.
*
* @return A pseudo-random 64-bit unsigned integer.
*/
uint64_t prand();
/**
* @brief Generates a random number within a specified range.
*
* Produces a random number in the inclusive range [min, max].
* Ensures uniform distribution by applying a modulo operation.
*
* @param min The lower bound of the range (inclusive).
* @param max The upper bound of the range (inclusive).
* @return A random number between min and max.
*/
uint64_t prand_range(uint64_t min, uint64_t max);
/**
* @brief Saves the current PRNG seed to flash memory.
*
* This function erases the designated flash page and writes the current seed
* to ensure the PRNG state persists across resets.
*/
void rand_save_to_flash();
/**
* @brief Re-seeds the PRNG seed state from either flash or BUILD_SEED.
*
* This function will not write to flash.
*/
void rand_reseed();