COMS W4995 C++ Deep Dive for C Programmers

Index of 2025-9/code/15

Parent directory
Makefile
rand1.cpp
rand2.cpp
rand3.cpp

Makefile

CC  = g++
CXX = g++

CFLAGS   = -g -Wall
CXXFLAGS = -g -Wall -std=c++20

executables = rand1 rand2 rand3

.PHONY: default
default: $(executables)

rand1: rand1.o

rand2: rand2.o

rand3: rand3.o

.PHONY: clean
clean:
	rm -f *~ a.out core *.o $(executables)

.PHONY: all
all: clean default

rand1.cpp

#include <cstdlib>
#include <cmath>
#include <iostream>
#include <iomanip>
#include <string>
#include <map>
#include <random>
#include <functional>
using namespace std;

int main() {

    // 1. Random number generator using C functions
    //
    srandom(time(nullptr)); // current time as seed
    auto engine = &random;  // random() in the C library as engine
    auto distro = [] (auto some_engine) {
        // get an unsigned random integer from engine,
        // and "distribute" it in [-10,10]
        return some_engine() % 21 - 10;
    };

    // 2. Random number generator in C++
    //
    // random_device rd;
    // random_device::result_type seed;
    // seed = (rd.entropy() > 0) ? rd() : time(nullptr);
    // default_random_engine engine { seed };
    // uniform_int_distribution<> distro { -10, 10 };

    map<int,int> m;
    for (int i = 0; i < 1'000'000; ++i) {
        int x = distro(engine);
        ++m[x];
    }
    for (const auto& [x, frequency] : m) {
        cout << setw(4) << x << '|' << frequency << '\n';
    }
}

rand2.cpp

#include <cstdlib>
#include <cmath>
#include <iostream>
#include <iomanip>
#include <string>
#include <map>
#include <random>
#include <functional>
using namespace std;

int main() {
    random_device rd;
    random_device::result_type seed;
    seed = (rd.entropy() > 0) ? rd() : time(nullptr);

    mt19937 engine { seed };
    normal_distribution<> distro { 0.0, 3.5 };

    map<int,int> m;
    int maximum = 0;

    for (int i = 0; i < 1'000'000; ++i) {

        int x = lround( distro(engine) );

        // auto generator = 
            // bind(distro, engine);
            // bind(distro, ref(engine));
            // [distro, engine] () { return distro(engine); };           // (L1)
            // [distro, engine] () mutable { return distro(engine); };   // (L2)
            // [=] () mutable { return distro(engine); };                // (L3)
            // [&distro, &engine] () { return distro(engine); };         // (L4)
            // [&] () { return distro(engine); };                        // (L5)
        // int x = lround( generator() );

        int frequency = ++m[x];
        maximum = max(maximum, frequency);
    }

    int bar_scale = maximum / 50;
    for (const auto& [x, frequency] : m) {
        cout << setw(4) << x << '|'
             << setw(7) << frequency << '|'
             << string(frequency / bar_scale, '*') << '\n';
    }
}

rand3.cpp

#include <cstdlib>
#include <cmath>
#include <iostream>
#include <iomanip>
#include <string>
#include <map>
#include <random>
#include <functional>
using namespace std;

int main() {
    random_device rd;
    random_device::result_type seed;
    seed = (rd.entropy() > 0) ? rd() : time(nullptr);

    mt19937 engine { seed };
    // default_random_engine engine { seed };

    cout << "typeid(engine).name():\n" << typeid(engine).name() << '\n';
    cout << "\nsizeof(engine): " << sizeof(engine) << '\n';
}