Skip to main content
Explorer
January 15, 2025
Solved

Random generator

  • January 15, 2025
  • 2 replies
  • 1066 views

Hi I am using STM32G030K8.

 

     as there is no TRNG options I am doing the ADC noise values to generate a 8 bit number.The MCU is supplied with 2.2V supply .I am using both mains and battery for my operation.However if battery is  removed Im not getting the ramndom number with mains supply .Can you please help this out 

void ADC_seed()
{
	HAL_ADC_Start(&hadc1);
	HAL_ADC_PollForConversion(&hadc1, HAL_MAX_DELAY);
	 seed = HAL_ADC_GetValue(&hadc1);
	 srand(seed);
}
uint32_t generate8DigitRandomNumber(void)
{
	
	ADC_seed();
	
}
 serialNumber = (rand() % 90000000) + 10000000;
 }
    This topic has been closed for replies.
    Best answer by mƎALLEm

    I do agree with @Andrew Neil. Reading a battery with ADC does not generate much randomness as the voltage is stable. Someone else has implemented it based on timers/RTC. Read this article.

    Or simply use DAC noise generator feature  and read it back with ADC. You didn't provide which MCU part number you are using so we could know if it's having DAC or not ..

    2 replies

    Super User
    January 15, 2025

    @meena wrote:

    However if battery is  removed Im not getting the random number with mains supply .


    What do you mean by, "not getting" the random number:

    • The ADC doesn't work at all ?
    • You get a value, but it isn't "random" ?

    I think your approach is flawed anyhow: you are just taking the whole ADC value - which is likely to be pretty much stable.

    The more usual approach is to just use the least significant bit(s) - as that's where the noise will be most obvious.

    What is your ADC connected to? Please show the schematic.

    Is it possible that the "mains" supply is earthed, so you see less noise in that case ... ?

     

    Is it mere coincidence that you're the second person asking this kind of question this week, or are you working with @undacmic ?

    https://community.st.com/t5/stm32-mcus-products/read-uninitialized-sram-values/m-p/761569 

    meenaAuthor
    Explorer
    January 15, 2025

    Hi, 

       I am getting the value 656284467 for serialNumber which is correct but I am getting this only if I connected my battery .I am working on a gear firmware uses Mains supply and Battery.I need this to be generated initially if the mains powered without battery .My serialNumber  is 0 if battery is not there.What may be the reason?

     

    I am working in  different may be a coincidence with the person mentioned .I am not aware about that

    Super User
    January 15, 2025

    @meena wrote:

    I am getting the value 656284467 for serialNumber which is correct 


    Pardon?

    If it's random, then it should not be a fixed number - surely?

    EDIT: But, surely, a Serial Number needs to be unique - not random?

     

    You didn't answer the questions:

    • What is your ADC connected to? Please show the schematic.

    • Is it possible that the "mains" supply is earthed, so you see less noise in that case ... ?

    Technical Moderator
    January 15, 2025

    @meena The approach of reading in something ‘random’ via an open ADC GPIO is widely used with Arduino, but has nothing to do with real randomness with sufficient entropy.

    True random numbers are actually not a trivial task, which was also the reason for developing the TRNG (True Random Number Generator), as found in some STM32. Depending on the entropy requirements, however, an approach such as a PNRG (Pseudo RNG) or as proposed by @mƎALLEm may well make sense.

    [edit] Please read the AN4230 for more information about random numbers, NIST and statistical background. [/edit]

    Regards
    /Peter