From bc6119282e36cd9392db81f85d227bb7367a57bd Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E4=BE=AF=E7=BA=A2=E5=8B=8B?= <houhongxun@kylinos.cn>
Date: Fri, 26 Apr 2024 10:44:42 +0800
Subject: [PATCH] use QRandomGenerator to generate random numbers
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Signed-off-by: 侯红勋 <houhongxun@kylinos.cn>
---
 screensaver/mbackground.cpp |  5 +++--
 screensaver/screensaver.cpp | 12 ++++++------
 screensaver/screensaver.h   |  2 ++
 src/configuration.cpp       |  7 ++++---
 4 files changed, 15 insertions(+), 11 deletions(-)

diff --git a/screensaver/mbackground.cpp b/screensaver/mbackground.cpp
index a1e6553..00efebe 100644
--- a/screensaver/mbackground.cpp
+++ b/screensaver/mbackground.cpp
@@ -23,6 +23,7 @@
 #include <QByteArray>
 #include <QImageReader>
 #include <QStringList>
+#include <QRandomGenerator>
 
 #include <ctime>
 #include "mbackground.h"
@@ -82,8 +83,8 @@ QString MBackground::getRand()
 {
     if(list.count() <= 0)
         return "";
-    qsrand(time(NULL));
-    currentIndex = qrand() % list.count();
+    QRandomGenerator rand1(time(NULL));
+    currentIndex = rand1.generate() % list.count();
 
     return list.at(currentIndex);
 }
diff --git a/screensaver/screensaver.cpp b/screensaver/screensaver.cpp
index c6ec6d4..f38b841 100644
--- a/screensaver/screensaver.cpp
+++ b/screensaver/screensaver.cpp
@@ -92,13 +92,13 @@ Screensaver::Screensaver(QWidget *parent):
   process(nullptr),
   screenLabel(nullptr),
   respondClick(false),
-  m_weatherManager(new WeatherManager(this))
+  m_weatherManager(new WeatherManager(this)),
+  m_rand(QTime(0,0,0).secsTo(QTime::currentTime()))
 {
     installEventFilter(this);
   //  setWindowFlags(Qt::X11BypassWindowManagerHint);
     setUpdateCenterWidget();
     setMouseTracking(true);
-    qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
 
     isCustom        =  configuration->getIsCustom();
     if(isCustom){
@@ -548,9 +548,9 @@ void Screensaver::setRandomPos()
     int x = 0;
     int y = 0;
     if(x2 > x1)
-        x = qrand()%(x2 - x1) + x1;
+        x = m_rand.generate()%(x2 - x1) + x1;
     if(y2 > y1)
-        y = qrand()%(y2 - y1) + y1;
+        y = m_rand.generate()%(y2 - y1) + y1;
 	
     myTextWidget->move(x,y);
 
@@ -650,7 +650,7 @@ void Screensaver::startSwitchImages()
             switchTimer = new QTimer(this);
             connect(switchTimer, &QTimer::timeout, this, [&]{
                 if(isAutoSwitch){
-                    int index = qrand() % imagePaths.count();
+                    int index = m_rand.generate() % imagePaths.count();
                     background  = QPixmap(imagePaths.at(index));
                 }else{
                     if(currentIndex>=imagePaths.count() - 1 || currentIndex<0){
@@ -700,7 +700,7 @@ void Screensaver::updateCenterWidget(int index)
         return;
 
     if(index<=1){
-        index = qrand() % qlist.count() + 1;
+        index = m_rand.generate() % qlist.count() + 1;
     }
     qsettings->beginGroup(QString::number(index));
     if(qsettings->contains("OL")){
diff --git a/screensaver/screensaver.h b/screensaver/screensaver.h
index 07cc994..ce5036c 100644
--- a/screensaver/screensaver.h
+++ b/screensaver/screensaver.h
@@ -30,6 +30,7 @@
 #include <QSplitterHandle>
 #include <QSplitter>
 #include <QTime>
+#include <QRandomGenerator>
 
 #include "sleeptime.h"
 #include "chinesedate.h"
@@ -143,6 +144,7 @@ private:
     QLabel *m_labelNoticeIcon;
     QLabel *m_labelNoticeMessage;
     int                 currentIndex = 0;
+    QRandomGenerator m_rand;
 protected:
     void paintEvent(QPaintEvent *event);
     void resizeEvent(QResizeEvent *event);
diff --git a/src/configuration.cpp b/src/configuration.cpp
index ac964a0..dcc6e6a 100644
--- a/src/configuration.cpp
+++ b/src/configuration.cpp
@@ -25,6 +25,7 @@
 #include <QGSettings>
 #include <QMimeType>
 #include <ctime>
+#include <QRandomGenerator>
 
 #include "commonfunc.h"
 #define GSETTINGS_SCHEMA_SCREENSAVER "org.ukui.screensaver"
@@ -143,11 +144,11 @@ ScreenSaver *Configuration::getScreensaver()
             saver->path = "/usr/lib/ukui-screensaver/ukui-screensaver-default";
             break;
         }
-        qsrand((unsigned)time(0));
-        int index = qrand() % themes.count();
+	QRandomGenerator rand1(time(0));
+        int index = rand1.generate() % themes.count();
         while(QString::compare(themes[index], "kyccss-personal-slideshow")==0)
         {
-            index = qrand() % themes.count();
+            index = rand1.generate() % themes.count();
         }
         saver->path = getXScreensaverPath(themes[index]);
         break;
-- 
2.43.0

