난수 생성
- random사용 -> 시드값 사용안하면 같은 값이 나올 수 있음
#include <iostream>
#include <random>
using namespace std;
int main() {
mt19937_64 mt_rand;
for (int i = 0; i < 10; i++) {
cout << mt_rand() << endl;
}
return 0;
}
시드값으로 난수생성
#include <iostream>
#include <random>
#include <chrono>
using namespace std;
int main() {
auto curTime = chrono::system_clock::now();
auto duration = curTime.time_since_epoch();
auto millis = chrono::duration_cast<chrono::milliseconds>(duration).count();
mt19937_64 mt_rand(millis);
for (int i = 0; i < 10; i++) {
cout << mt_rand() << endl;
}
return 0;
}
하드웨어 엔트로피로 난수생성
- 마우스 움직임, 커서위치, 키보드 입력, 디스크 I/O등 외부 요인을 활용하여 엔트로피 수집
- random_device는 대체로 mt19937 엔진보다 느림
#include <iostream>
#include <random>
using namespace std;
int main() {
random_device rng;
for (int i = 0; i < 10; i++) {
auto result = rng();
cout << result << endl;
}
return 0;
}
- random_device는 OS/하드웨어가 주는 엔트로피(진짜 랜덤성)에 접근하는 인터페이스
- seed는 “시작 버튼”이고,
이후에는 알고리즘이 자기 상태를 계속 업데이트함
- random_device = “랜덤 재료 공급기”
mt19937 = “요리사 (대량 생산용)”
- random_device = seed 공급
mt19937 = 빠른 난수 생성기
distribution = 원하는 분포
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<int> dist(1, 100);
int x = dist(gen);
수학함수
#include <iostream>
#include <cmath>
#include <numbers>
using namespace std;
int main() {
double x = 2;
double y = 3;
cout << "x = " << x << ", y = " << y << endl;
cout << "pow(x, y) = " << pow(x, y) << endl;
cout << "sqrt(x) = " << sqrt(x) << endl;
cout << "log(x) = " << log(x) << endl;
cout << endl;
x = 2.847;
y = -3.234;
cout << "x = " << x << ", y = " << y << endl;
cout << "ceil(x) = " << ceil(x) << ", ceil(y)" << ceil(y) << endl;
cout << "floor(x) = " << floor(x) << ", floor(y)" << floor(y) << endl;
cout << "round(x) = " << round(x) << ", round(y)" << round(y) << endl;
cout << "abs(x) = " << abs(x) << ", abs(y)" << abs(y) << endl;
cout << endl;
cout << "PI = " << numbers::pi << endl;
cout << "sin(PI/3) = " << sin(numbers::pi / 3) << endl;
cout << "cos(PI/3) = " << cos(numbers::pi / 3) << endl;
cout << "tan(PI/3) = " << tan(numbers::pi / 3) << endl;
return 0;
}
복사함수
- 얕은 복사 : 주소값을 복사
- 깊은 복사 : 실제 값을 새로운 메로리 공간에 복사
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
struct Person {
string name;
int age;
float height;
float weight;
};
void print_person_all(vector<Person>& vec) {
for (vector<Person>::iterator it = vec.begin(); it != vec.end(); it++) {
cout << "이름: " << it->name << "\t > " << "나이: " << it->age << ", 키: " << it->height << ", 몸무게: " << it->weight << endl;
}
}
int main() {
Person p[5] = {
{"Brain", 24, 180, 70}
};
vector<Person> from_vector;
from_vector.push_back(p[0]);
cout << "-----from_vector-----" << endl;
print_person_all(from_vector);
cout << endl;
vector<Person> to_vector;
copy(from_vector.begin(), from_vector.end(), back_inserter(to_vector));
cout << "-----to_vector-----" << endl;
print_person_all(to_vector);
cout << endl;
from_vector[0].name = "Chris";
from_vector[0].age = 5;
from_vector[0].height = 110;
from_vector[0].weight = 20;
cout << "-----from_vector-----" << endl;
print_person_all(from_vector);
cout << endl;
cout << "-----to_vector-----" << endl;
print_person_all(to_vector);
cout << endl;
return 0;
}
- 깊은복사를 했으므로 from_vector를 변경해도 to_vector는 변경되지 않음