From 090936e42421191f21ee946e1c04c1e36c4f9a5f Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Boric Date: Fri, 14 May 2021 17:27:38 +0200 Subject: [PATCH] Userland: Replace arc4random() with get_random() --- Userland/Games/Breakout/Game.cpp | 7 ++++--- Userland/Games/Chess/ChessWidget.cpp | 3 ++- Userland/Games/Conway/Game.cpp | 3 ++- Userland/Games/Pong/Game.cpp | 7 ++++--- Userland/Libraries/LibCrypto/PK/RSA.cpp | 2 +- Userland/Libraries/LibJS/Runtime/MathObject.cpp | 3 ++- Userland/Services/ChessEngine/ChessEngine.cpp | 3 ++- Userland/Utilities/mktemp.cpp | 3 ++- 8 files changed, 19 insertions(+), 12 deletions(-) diff --git a/Userland/Games/Breakout/Game.cpp b/Userland/Games/Breakout/Game.cpp index a1ba3e6051..b812be602f 100644 --- a/Userland/Games/Breakout/Game.cpp +++ b/Userland/Games/Breakout/Game.cpp @@ -6,6 +6,7 @@ #include "Game.h" #include "LevelSelectDialog.h" +#include #include #include #include @@ -191,11 +192,11 @@ void Game::reset_ball() { int position_x_min = (game_width / 2) - 50; int position_x_max = (game_width / 2) + 50; - int position_x = arc4random() % (position_x_max - position_x_min + 1) + position_x_min; + int position_x = get_random() % (position_x_max - position_x_min + 1) + position_x_min; int position_y = 200; - int velocity_x = arc4random() % 3 + 1; + int velocity_x = get_random() % 3 + 1; int velocity_y = 3 + (3 - velocity_x); - if (arc4random() % 2) + if (get_random() % 2) velocity_x = velocity_x * -1; m_ball = {}; diff --git a/Userland/Games/Chess/ChessWidget.cpp b/Userland/Games/Chess/ChessWidget.cpp index 778c035d04..d501a32005 100644 --- a/Userland/Games/Chess/ChessWidget.cpp +++ b/Userland/Games/Chess/ChessWidget.cpp @@ -6,6 +6,7 @@ #include "ChessWidget.h" #include "PromotionDialog.h" +#include #include #include #include @@ -382,7 +383,7 @@ void ChessWidget::reset() m_playback_move_number = 0; m_board_playback = Chess::Board(); m_board = Chess::Board(); - m_side = (arc4random() % 2) ? Chess::Color::White : Chess::Color::Black; + m_side = (get_random() % 2) ? Chess::Color::White : Chess::Color::Black; m_drag_enabled = true; input_engine_move(); update(); diff --git a/Userland/Games/Conway/Game.cpp b/Userland/Games/Conway/Game.cpp index 3e2de50062..054bbde4e6 100644 --- a/Userland/Games/Conway/Game.cpp +++ b/Userland/Games/Conway/Game.cpp @@ -5,6 +5,7 @@ */ #include "Game.h" +#include #include #include #include @@ -30,7 +31,7 @@ void Game::seed_universe() { for (int y = 0; y < m_rows; y++) { for (int x = 0; x < m_columns; x++) { - m_universe[y][x] = (arc4random() % 2) ? 1 : 0; + m_universe[y][x] = (get_random() % 2) ? 1 : 0; } } } diff --git a/Userland/Games/Pong/Game.cpp b/Userland/Games/Pong/Game.cpp index 8e889ff0be..4fd689a482 100644 --- a/Userland/Games/Pong/Game.cpp +++ b/Userland/Games/Pong/Game.cpp @@ -5,6 +5,7 @@ */ #include "Game.h" +#include namespace Pong { @@ -100,11 +101,11 @@ void Game::reset_ball(int serve_to_player) { int position_y_min = (game_width / 2) - 50; int position_y_max = (game_width / 2) + 50; - int position_y = arc4random() % (position_y_max - position_y_min + 1) + position_y_min; + int position_y = get_random() % (position_y_max - position_y_min + 1) + position_y_min; int position_x = (game_height / 2); - int velocity_y = arc4random() % 3 + 1; + int velocity_y = get_random() % 3 + 1; int velocity_x = 5 + (5 - velocity_y); - if (arc4random() % 2) + if (get_random() % 2) velocity_y = velocity_y * -1; if (serve_to_player == 2) velocity_x = velocity_x * -1; diff --git a/Userland/Libraries/LibCrypto/PK/RSA.cpp b/Userland/Libraries/LibCrypto/PK/RSA.cpp index e426d755ab..97e2480a53 100644 --- a/Userland/Libraries/LibCrypto/PK/RSA.cpp +++ b/Userland/Libraries/LibCrypto/PK/RSA.cpp @@ -339,7 +339,7 @@ void RSA_PKCS1_EME::encrypt(ReadonlyBytes in, Bytes& out) ps.resize(ps_length); fill_with_random(ps.data(), ps_length); - // since arc4random can create zeros (shocking!) + // since fill_with_random can create zeros (shocking!) // we have to go through and un-zero the zeros for (size_t i = 0; i < ps_length; ++i) while (!ps[i]) diff --git a/Userland/Libraries/LibJS/Runtime/MathObject.cpp b/Userland/Libraries/LibJS/Runtime/MathObject.cpp index 16489c7c19..c48c09916d 100644 --- a/Userland/Libraries/LibJS/Runtime/MathObject.cpp +++ b/Userland/Libraries/LibJS/Runtime/MathObject.cpp @@ -6,6 +6,7 @@ */ #include +#include #include #include #include @@ -86,7 +87,7 @@ JS_DEFINE_NATIVE_FUNCTION(MathObject::abs) JS_DEFINE_NATIVE_FUNCTION(MathObject::random) { #ifdef __serenity__ - double r = (double)arc4random() / (double)UINT32_MAX; + double r = (double)get_random() / (double)UINT32_MAX; #else double r = (double)rand() / (double)RAND_MAX; #endif diff --git a/Userland/Services/ChessEngine/ChessEngine.cpp b/Userland/Services/ChessEngine/ChessEngine.cpp index 0056254fc1..2876fd2453 100644 --- a/Userland/Services/ChessEngine/ChessEngine.cpp +++ b/Userland/Services/ChessEngine/ChessEngine.cpp @@ -7,6 +7,7 @@ #include "ChessEngine.h" #include "MCTSTree.h" #include +#include #include using namespace Chess::UCI; @@ -34,7 +35,7 @@ void ChessEngine::handle_go(const GoCommand& command) // FIXME: Add different ways to terminate search. VERIFY(command.movetime.has_value()); - srand(arc4random()); + srand(get_random()); Core::ElapsedTimer elapsed_time; elapsed_time.start(); diff --git a/Userland/Utilities/mktemp.cpp b/Userland/Utilities/mktemp.cpp index 376ba6a841..daf4f77168 100644 --- a/Userland/Utilities/mktemp.cpp +++ b/Userland/Utilities/mktemp.cpp @@ -5,6 +5,7 @@ */ #include +#include #include #include #include @@ -23,7 +24,7 @@ static char* generate_random_filename(const char* pattern) for (auto i = pattern_length - 1; i >= 0; --i) { if (pattern[i] != 'X') break; - new_filename[i] = random_characters[(arc4random() % (sizeof(random_characters) - 1))]; + new_filename[i] = random_characters[(get_random() % (sizeof(random_characters) - 1))]; } return new_filename;