From edf766481db37f6af8a6683e0ec1548972b9157d Mon Sep 17 00:00:00 2001 From: Scott Giese <scttgs0@gmail.com> Date: Sun, 21 Feb 2021 01:26:50 -0600 Subject: [PATCH] Modernize: genTransponderCode() update to c++ --- src/ATC/trafficcontrol.cxx | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/src/ATC/trafficcontrol.cxx b/src/ATC/trafficcontrol.cxx index 66f9a0a8e..f23698cc7 100644 --- a/src/ATC/trafficcontrol.cxx +++ b/src/ATC/trafficcontrol.cxx @@ -24,6 +24,7 @@ #include <algorithm> #include <cstdio> +#include <random> #include <osg/Geode> #include <osg/Geometry> @@ -868,14 +869,19 @@ string FGATCController::formatATCFrequency3_2(int freq) // The current version just returns a random string of four octal numbers. string FGATCController::genTransponderCode(const string& fltRules) { - if (fltRules == "VFR") { + if (fltRules == "VFR") return string("1200"); - } else { - char buffer[5]; - snprintf(buffer, sizeof(buffer), "%d%d%d%d", rand() % 8, rand() % 8, rand() % 8, - rand() % 8); - return string(buffer); - } + + std::default_random_engine generator; + std::uniform_int_distribution<unsigned> distribution(0, 7); + + unsigned val = ( + distribution(generator) * 1000 + + distribution(generator) * 100 + + distribution(generator) * 10 + + distribution(generator)); + + return std::to_string(val); } void FGATCController::eraseDeadTraffic(TrafficVector& vec)