Stm32 produces random numbers __stm32

Source: Internet
Author: User
Tags rand random seed

Stm32 uses the rand () function to generate random numbers, and in OS Programming, Srand () random seeds are set, in fact, the random seed is the value of time (), because the value is different, so each random number is different.

Then we need to simulate such a random seed in the stm32.

1--can take ADC sampling values and must be an empty foot (no GND&VCC)

2--can use the timer's count value

Upload part of the code

		/* You
					must refresh the seed each time. The random number will not be the same each time
					Srand () in #include "Stdlib.h"
					rand () in #include "time.h"
			* * srand
			
			(timer_count_value);	Random seed timer_count_value is the counting value of the timer,
			
			num  = rand ()% 128;    Generates a random integer within 0-128,
			
			printf ("<<---os_run:%d--->>\r\n", num);

Print results

<<---os_run:0--->>
<<---os_run:106--->>
<<---os_run:84--->>
<<---os_run:62--->>
<<---os_run:40--->>
<<---os_run:18--->>
<<---os_run:124--->>
<<---os_run:29--->>
<<---os_run:7--->>
<<---os_run:113--->>
<<---os_run:17--->>
<<---os_run:123--->>
<<---os_run:28--->>
<<---os_run:6--->>
<<---os_run:57--->>
<<---os_run:35--->>



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.