Browse Source

Userland: Replace arc4random() with get_random<u32>()

Jean-Baptiste Boric 4 years ago
parent
commit
090936e424

+ 4 - 3
Userland/Games/Breakout/Game.cpp

@@ -6,6 +6,7 @@
 
 
 #include "Game.h"
 #include "Game.h"
 #include "LevelSelectDialog.h"
 #include "LevelSelectDialog.h"
+#include <AK/Random.h>
 #include <LibGUI/Application.h>
 #include <LibGUI/Application.h>
 #include <LibGUI/MessageBox.h>
 #include <LibGUI/MessageBox.h>
 #include <LibGUI/Painter.h>
 #include <LibGUI/Painter.h>
@@ -191,11 +192,11 @@ void Game::reset_ball()
 {
 {
     int position_x_min = (game_width / 2) - 50;
     int position_x_min = (game_width / 2) - 50;
     int position_x_max = (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<u32>() % (position_x_max - position_x_min + 1) + position_x_min;
     int position_y = 200;
     int position_y = 200;
-    int velocity_x = arc4random() % 3 + 1;
+    int velocity_x = get_random<u32>() % 3 + 1;
     int velocity_y = 3 + (3 - velocity_x);
     int velocity_y = 3 + (3 - velocity_x);
-    if (arc4random() % 2)
+    if (get_random<u32>() % 2)
         velocity_x = velocity_x * -1;
         velocity_x = velocity_x * -1;
 
 
     m_ball = {};
     m_ball = {};

+ 2 - 1
Userland/Games/Chess/ChessWidget.cpp

@@ -6,6 +6,7 @@
 
 
 #include "ChessWidget.h"
 #include "ChessWidget.h"
 #include "PromotionDialog.h"
 #include "PromotionDialog.h"
+#include <AK/Random.h>
 #include <AK/String.h>
 #include <AK/String.h>
 #include <LibCore/DateTime.h>
 #include <LibCore/DateTime.h>
 #include <LibCore/File.h>
 #include <LibCore/File.h>
@@ -382,7 +383,7 @@ void ChessWidget::reset()
     m_playback_move_number = 0;
     m_playback_move_number = 0;
     m_board_playback = Chess::Board();
     m_board_playback = Chess::Board();
     m_board = Chess::Board();
     m_board = Chess::Board();
-    m_side = (arc4random() % 2) ? Chess::Color::White : Chess::Color::Black;
+    m_side = (get_random<u32>() % 2) ? Chess::Color::White : Chess::Color::Black;
     m_drag_enabled = true;
     m_drag_enabled = true;
     input_engine_move();
     input_engine_move();
     update();
     update();

+ 2 - 1
Userland/Games/Conway/Game.cpp

@@ -5,6 +5,7 @@
  */
  */
 
 
 #include "Game.h"
 #include "Game.h"
+#include <AK/Random.h>
 #include <LibGUI/Painter.h>
 #include <LibGUI/Painter.h>
 #include <stdlib.h>
 #include <stdlib.h>
 #include <time.h>
 #include <time.h>
@@ -30,7 +31,7 @@ void Game::seed_universe()
 {
 {
     for (int y = 0; y < m_rows; y++) {
     for (int y = 0; y < m_rows; y++) {
         for (int x = 0; x < m_columns; x++) {
         for (int x = 0; x < m_columns; x++) {
-            m_universe[y][x] = (arc4random() % 2) ? 1 : 0;
+            m_universe[y][x] = (get_random<u32>() % 2) ? 1 : 0;
         }
         }
     }
     }
 }
 }

+ 4 - 3
Userland/Games/Pong/Game.cpp

@@ -5,6 +5,7 @@
  */
  */
 
 
 #include "Game.h"
 #include "Game.h"
+#include <AK/Random.h>
 
 
 namespace Pong {
 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_min = (game_width / 2) - 50;
     int position_y_max = (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<u32>() % (position_y_max - position_y_min + 1) + position_y_min;
     int position_x = (game_height / 2);
     int position_x = (game_height / 2);
-    int velocity_y = arc4random() % 3 + 1;
+    int velocity_y = get_random<u32>() % 3 + 1;
     int velocity_x = 5 + (5 - velocity_y);
     int velocity_x = 5 + (5 - velocity_y);
-    if (arc4random() % 2)
+    if (get_random<u32>() % 2)
         velocity_y = velocity_y * -1;
         velocity_y = velocity_y * -1;
     if (serve_to_player == 2)
     if (serve_to_player == 2)
         velocity_x = velocity_x * -1;
         velocity_x = velocity_x * -1;

+ 1 - 1
Userland/Libraries/LibCrypto/PK/RSA.cpp

@@ -339,7 +339,7 @@ void RSA_PKCS1_EME::encrypt(ReadonlyBytes in, Bytes& out)
     ps.resize(ps_length);
     ps.resize(ps_length);
 
 
     fill_with_random(ps.data(), 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
     // we have to go through and un-zero the zeros
     for (size_t i = 0; i < ps_length; ++i)
     for (size_t i = 0; i < ps_length; ++i)
         while (!ps[i])
         while (!ps[i])

+ 2 - 1
Userland/Libraries/LibJS/Runtime/MathObject.cpp

@@ -6,6 +6,7 @@
  */
  */
 
 
 #include <AK/Function.h>
 #include <AK/Function.h>
+#include <AK/Random.h>
 #include <LibJS/Runtime/GlobalObject.h>
 #include <LibJS/Runtime/GlobalObject.h>
 #include <LibJS/Runtime/MathObject.h>
 #include <LibJS/Runtime/MathObject.h>
 #include <math.h>
 #include <math.h>
@@ -86,7 +87,7 @@ JS_DEFINE_NATIVE_FUNCTION(MathObject::abs)
 JS_DEFINE_NATIVE_FUNCTION(MathObject::random)
 JS_DEFINE_NATIVE_FUNCTION(MathObject::random)
 {
 {
 #ifdef __serenity__
 #ifdef __serenity__
-    double r = (double)arc4random() / (double)UINT32_MAX;
+    double r = (double)get_random<u32>() / (double)UINT32_MAX;
 #else
 #else
     double r = (double)rand() / (double)RAND_MAX;
     double r = (double)rand() / (double)RAND_MAX;
 #endif
 #endif

+ 2 - 1
Userland/Services/ChessEngine/ChessEngine.cpp

@@ -7,6 +7,7 @@
 #include "ChessEngine.h"
 #include "ChessEngine.h"
 #include "MCTSTree.h"
 #include "MCTSTree.h"
 #include <AK/Debug.h>
 #include <AK/Debug.h>
+#include <AK/Random.h>
 #include <LibCore/ElapsedTimer.h>
 #include <LibCore/ElapsedTimer.h>
 
 
 using namespace Chess::UCI;
 using namespace Chess::UCI;
@@ -34,7 +35,7 @@ void ChessEngine::handle_go(const GoCommand& command)
     // FIXME: Add different ways to terminate search.
     // FIXME: Add different ways to terminate search.
     VERIFY(command.movetime.has_value());
     VERIFY(command.movetime.has_value());
 
 
-    srand(arc4random());
+    srand(get_random<u32>());
 
 
     Core::ElapsedTimer elapsed_time;
     Core::ElapsedTimer elapsed_time;
     elapsed_time.start();
     elapsed_time.start();

+ 2 - 1
Userland/Utilities/mktemp.cpp

@@ -5,6 +5,7 @@
  */
  */
 
 
 #include <AK/LexicalPath.h>
 #include <AK/LexicalPath.h>
+#include <AK/Random.h>
 #include <LibCore/ArgsParser.h>
 #include <LibCore/ArgsParser.h>
 #include <fcntl.h>
 #include <fcntl.h>
 #include <stdio.h>
 #include <stdio.h>
@@ -23,7 +24,7 @@ static char* generate_random_filename(const char* pattern)
     for (auto i = pattern_length - 1; i >= 0; --i) {
     for (auto i = pattern_length - 1; i >= 0; --i) {
         if (pattern[i] != 'X')
         if (pattern[i] != 'X')
             break;
             break;
-        new_filename[i] = random_characters[(arc4random() % (sizeof(random_characters) - 1))];
+        new_filename[i] = random_characters[(get_random<u32>() % (sizeof(random_characters) - 1))];
     }
     }
 
 
     return new_filename;
     return new_filename;