<언어>/[C++]

[C++] random generator

콜리브리 2020. 9. 10. 21:58

rand() is now considered obsolete; a pseudo-random generator that produces repeating, non-random numbers in low order bits.

 

Use Mersene twister random generators and proper distribution to generate functional random #

 

 

- Generate random 'string'

stackoverflow.com/questions/47977829/generate-a-random-string-in-c11/50556436#50556436

 

generate a random string in C++11?

I need help with generating a random string using C++11. I don't know how to continue with that, if you can help me please. #include char * random_string() { static const ...

stackoverflow.com

- Mersenne twister와 discrete distribution 을 이용한 가챠 구현

출처: https://kindtis.tistory.com/601 [Game Programmer Life]

 

discrete_distribution 분포 클래스를 선언할 때, 가중치를 넣어줍니다. 예제와 같이 50, 30, 15, 5 순으로 입력해주면,

 

0이 되올 확률 50,

1이 나올 확률 30,

2가 나올 확률 15,

5가 나올 확률 5

#include <random>
std::random_device rd;
std::mt19937 gen(rd());
std::discrete_distribution<> d({ 50, 30, 15, 5 });
std::map<int, int> m;

for (int n = 0; n < 10000; ++n) {
	++m[d(gen)];
}
for (auto p : m) {
	std::cout << p.first << " generated " << p.second << " times\n";
}