17 Ocak 2019 Perşembe

std::random_device Sınıfı - Yaratması Maliyetlidir

Giriş
Şu satırı dahil ederiz.
#include<random>
Açıklaması şöyle
std::random_device may be expensive to create. It may open a device feeding it data, and that takes some time. It can also be expensive to call because it requires entropy to deliver a truly random number, not a pseudo random number.
Donanım tabanlıdır. Eski bilgisayarlarda donanım tabanlı dağılım için ne yapıldığını anlatan bir yazı şöyle.

Eğer donanım desteği yoksa algoritma tabanlı bir üretece dönüşür. Uyarı vermeden yapılan bu geçiş istenmeyen sonuçlara sebep olabilir. Açıklaması şöyle.
std::random_device, introduced in C++11, is not recommended because its specification leaves considerably much to be desired. For example, std::random_device can fall back to a pseudorandom number generator of unspecified quality without much warning.

Constructor
Şöyle yaparız.
std::random_device rd;
entropy metodu
entropy şu anlama gelir.
entropy is the measure of surprise
Windows'ta bu metod hep 32 döner.

Linux'ta işletim sisteminin entropisini görmek için şöyle yaparız.
cat /proc/sys/kernel/random/entropy_avail
Eğer donanım tabanlı bir üreteç yoksa yine yazılım tabanlı bir üreteç kullanılır. Açıklaması şöyle.
std::random_device may be implemented in terms of an implementation-defined pseudo-random number engine if a non-deterministic source (e.g. a hardware device) is not available to the implementation. In this case each std::random_device object may generate the same number sequence.
Bu durumda sınıfın entropy metodu 0 döner. Şöyle yaparız.
std::random_device rd;
std::cout << rd.entropy();
Çıktı olarak şunu alırız.
0
Diğer Üreteçleri İlklendirme - mt19937
Şöyle yaparız.
std::random_device rd;
std::mt19937 eng(rd());
Bu kod eleştirilere maruz kalıyor. Sebepleri şöyle. Tüm bunlar şu anlama geliyor mt19937'yi ilklendirmek için random_device yeterli değil.
1. rd() returns a single unsigned int. This has at least 16 bits and probably 32. That's not enough to seed MT's 19937 bits of state.

2. Using std::mt19937 gen(rd());gen() (seeding with 32 bits and looking at the first output) doesn't give a good output distribution. 7 and 13 can never be the first output. Two seeds produce 0. Twelve seeds produce 1226181350.

3. std::random_device can be, and sometimes is, implemented as a simple PRNG with a fixed seed. It might therefore produce the same sequence on every run. (Link) This is even worse than time(NULL).



Hiç yorum yok:

Yorum Gönder