본문 바로가기
아두이노

randomSeed

by 잡T 2020. 1. 4.

random(max) 또는 random(min, max) 함수는 난수를 생성해 주는 함수이다. (max 값만 설정하면 0 ~ max 범위 내의 숫자를 무작위로 추출해 낸다.)   그런데 아두이노에서 난수를 생성해 보면 매번 똑같은 숫자가 랜덤 생성 된다.   아두이노 참조 사이트의 random 함수 예제 아래 주의와 경고 항목을 살펴보자.

 

https://www.arduino.cc/reference/ko/language/functions/random-numbers/random/  

 

주의와 경고 If it is important for a sequence of values generated by random() to differ, on subsequent executions of a sketch, use randomSeed() to initialize the random number generator with a fairly random input, such as analogRead() on an unconnected pin.


random()에 의해 생성된 값의 순서가 다른 것이 중요한 경우, 스케치의 후속 실행 시 RandomSeed()를 사용하여 연결되지 않은 핀의 analogRead()와 같은 상당히 무작위 입력으로 랜덤 번호 생성기를 초기화한다.


   randomSeed(analogRead(0));  // analogRead(0)의 값은 플로팅 현상으로 인해 0~1,023사이의 무작위 값을 반환한다.

'아두이노' 카테고리의 다른 글

555 timer 쌍안정 모드  (0) 2020.01.31
슬라이드 스위치 사용 (풀다운)  (0) 2020.01.27
아두이노 플로팅 현상  (0) 2020.01.27
led 깜박이기  (0) 2020.01.27
아두이노 예제 1  (0) 2020.01.27

댓글