C++Guns – RoboBlog blogging the bot

30.05.2020

C++ Guns - Spassvergleich Fortran77 Fortran95 C++11

Filed under: Allgemein — Tags: , — Thomas @ 16:05

Links oben: Original Fortran77 Code.
Rechts oben: Fortran95. Keine GOTOs, keine Labels, keine Spaltenbeschränkung, Sortieralgorithmus identifiziert
Unten: C++. Standard Sortieralgorithmus, keine expliziten Array Zugriffe

spassverglechfortrancpp

Click to enlarge

Ja, die genaue formatierte Ausgabe auf die richtige Nachkommastelle ist im C++ Code nicht enthalten. Diese formatierte Ausgabe in Text Dateien ist auch heute nicht mehr relevant.

06.05.2020

C++ Guns: generate random bits - std C++ way

Filed under: Allgemein — Tags: — Thomas @ 05:05

I found only one std C++ way to generator random bool value: using std::bernoulli_distribution. It returns bool values and has a default constructor with probability p=0.5. So the generated boolen are "uniform" distributed.

This is perhaps the slowest variant. The internet is full of better ideas. Some use std C++ random classes but they all need some extra code to work. For example: cast int to bool, extract one bit from a 32bit integer value.

#include <random>

std::random_device rd;
std::mt19937 g(rd());
std::bernoulli_distribution dis_true_false;

if(dis_true_false(g)) {
   // heads
} else {
   // tails
}

Powered by WordPress