cancel
Showing results for 
Search instead for 
Did you mean: 

Random generator

meena
Associate III

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;
 }
5 REPLIES 5

@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 

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

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 ..

To give better visibility on the answered topics, please click on "Accept as Solution" on the reply which solved your issue or answered your question.
PS: This is a collaborative space. So please be polite in your reply. Otherwise, it will be reported as inappropriate and you will be permanently blacklisted from my help/support.

@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 ... ?

Peter BENSCH
ST Employee

@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 @SofLit may well make sense.

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

Regards
/Peter

In order to give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.