• 카테고리

    질문 & 답변
  • 세부 분야

    프로그래밍 언어

  • 해결 여부

    해결됨

random_device와 mt19937의 차이

21.12.28 20:30 작성 조회수 509

1

random_device와 mt19937의 차이를 정확하게 잘 이해가 안되서 그런데

random_device가 srand()처럼 seed값을 바꿔주고

mt19937이 rand()처럼 난수의 값을 반환? 하는 역할을 하는건가요?

 

답변 1

답변을 작성해보세요.

1

강민철님의 프로필

강민철

2021.12.29

안녕하세요 :)

random_device도 난수를 발생시키고 mt19937도 난수를 발생시킵니다.

아래의 코드를 실행해보시면 random_device로도 난수가 발생하고 mt19937로도 난수를 발생시킨다는 걸 알 수 있습니다.

---

#include <iostream>
#include <random>
using namespace std;

int main()
{
	random_device rng;
	for (int i = 0; i < 10; i++)
		cout << rng() << endl;
}

---

#include <iostream>
#include <random>
using namespace std;


int main() 
{
	mt19937 mtRand;
	for (int i = 0; i < 10; i++) 
		cout << mtRand() << endl;
	
	return 0;

}

 

강의 속 예제에서는 random_device의 값을 마치 seed처럼 사용한 것입니다.

 

감사합니다