OSDN Git Service

kdeplasma-addons: make use of KRandom::randomMax()
authorIvailo Monev <xakepa10@gmail.com>
Thu, 29 Sep 2022 23:04:12 +0000 (02:04 +0300)
committerIvailo Monev <xakepa10@gmail.com>
Thu, 29 Sep 2022 23:04:12 +0000 (02:04 +0300)
Signed-off-by: Ivailo Monev <xakepa10@gmail.com>
kdeplasma-addons/applets/life/life.cpp
kdeplasma-addons/applets/paste/pastemacroexpander.cpp
kdeplasma-addons/dataengines/potd/flickrprovider.cpp
kdeplasma-addons/wallpapers/virus/alife.cpp

index 215013a..5952879 100644 (file)
@@ -28,6 +28,7 @@
 #include <Plasma/Theme>
 #include <KConfigDialog>
 #include <KDebug>
+#include <KRandom>
 
 Life::Life(QObject *parent, const QVariantList &args)
     : Plasma::Applet(parent, args),
@@ -250,7 +251,7 @@ void Life::initGame()
 void Life::resetGame()
 {
     for (int i = 0; i < (m_cellsArrayHeight * m_cellsArrayWidth); i++){
-        m_cells[i] = (rand() % 100) < m_popDensityNumber ? 1 : 0;
+        m_cells[i] = (KRandom::randomMax(100) < m_popDensityNumber ? 1 : 0);
     }
 
     if (m_reflectHorizontal){
index 747cd61..e2250d5 100644 (file)
@@ -184,7 +184,7 @@ QString PasteMacroExpander::password(const QString& args)
             rand = KRandom::random();
         } while (rand >= top);
 
-        result += chars[rand % setSize];
+        result += chars[KRandom::randomMax(setSize)];
     }
     //kDebug() << result;
     return result;
index f16200a..06fdcac 100644 (file)
@@ -37,7 +37,6 @@ class FlickrProvider::Private
     Private( FlickrProvider *parent )
       : mParent( parent )
     {
-        qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
     }
 
     void pageRequestFinished( KJob* );
index 1f0d0eb..b4c7603 100644 (file)
@@ -8,8 +8,9 @@
 */
 
 #include "alife.h" 
-#include <QtCore/qdatetime.h>
+#include <QDateTime>
 #include <KDebug>
+#include <KRandom>
 
 #define VIRUS_GENOME_SIZE 38
 #define MAX_AGE 8
@@ -90,8 +91,8 @@ void Alife::initVirus(){
 //create the needed viruses so that we have in total "amount" viruses
 void Alife::createViruses(int amount){
     for(int i = m_livingCells.size(); i < amount; i++) {
-        int x = rand() % m_width;
-        int y = rand() % m_height;
+        int x = KRandom::randomMax(m_width);
+        int y = KRandom::randomMax(m_height);
         
         struct cell *temp = &m_cells[x][y];
         //cell already alive
@@ -105,10 +106,10 @@ void Alife::createViruses(int amount){
 
             //initial code
             for(int i = 0; i < 7; i++) {
-                temp->code[i] = rand()%12;
+                temp->code[i] = KRandom::randomMax(12);
             }
 
-            temp->code[rand()%7] = 7; //cheating, jumpstart evolution
+            temp->code[KRandom::randomMax(7)] = 7; //cheating, jumpstart evolution
 
             /*temp->code[0] = 4;
             temp->code[1] = 12;
@@ -155,7 +156,7 @@ void Alife::resetCell(struct cell *temp)
 //REMINDER: Update if a new op is added
 uchar Alife::randomCode()
 {
-    return rand() % 20;
+    return KRandom::randomMax(20);
 }
 
 //Executes the code of a certain living cell
@@ -188,7 +189,7 @@ void Alife::executeCell(int id)
                 stop = true;
             break;
             case 1://random value
-                reg = rand() % 4;
+                reg = KRandom::randomMax(4);
             break;
             case 2: //change direction
                 facing = reg & 3;
@@ -381,7 +382,7 @@ bool Alife::reproduce(struct cell* cell, int direction, QRgb color)
         //give a unfair advantage to darker places
         int prob = (((qRed(color) + qGreen(color) + qBlue(color))/ 255.)+1);// 0 - 765
 
-        if(rand() % prob  != 0){
+        if(KRandom::randomMax(prob)  != 0){
             return false;
         }
 
@@ -395,26 +396,26 @@ bool Alife::reproduce(struct cell* cell, int direction, QRgb color)
 
         memcpy(newCell->code,cell->code,VIRUS_GENOME_SIZE);
 
-        int mutate = rand() % 3;
+        int mutate = KRandom::randomMax(3);
         if(mutate) {
             //normal mutation
-            int mutations = rand() % 5;
+            int mutations = KRandom::randomMax(5);
             for(int i = 0; i < mutations; i++) {
-                int index = rand() % VIRUS_GENOME_SIZE;
+                int index = KRandom::randomMax(VIRUS_GENOME_SIZE);
                 newCell->code[index] = randomCode();
             }
 
-            int duplication = rand() % 3;
+            int duplication = KRandom::randomMax(3);
             for(int i = 0; i < duplication; i++) {
-                int start = rand() % VIRUS_GENOME_SIZE;
-                int end = start + rand() % ( VIRUS_GENOME_SIZE - start);
+                int start = KRandom::randomMax(VIRUS_GENOME_SIZE);
+                int end = start + KRandom::randomMax( VIRUS_GENOME_SIZE - start);
                 memcpy(&newCell->code[end],&cell->code[start],VIRUS_GENOME_SIZE - end);
             }
 
-            int deletion = rand() % 3;
+            int deletion = KRandom::randomMax(3);
             for(int i = 0; i < deletion; i++) {
-                int start = rand() % VIRUS_GENOME_SIZE;
-                int end = start + rand() % ( VIRUS_GENOME_SIZE - start);
+                int start = KRandom::randomMax(VIRUS_GENOME_SIZE);
+                int end = start + KRandom::randomMax( VIRUS_GENOME_SIZE - start);
                 memcpy(&newCell->code[start],&cell->code[end],VIRUS_GENOME_SIZE - end);
                 memset(&newCell->code[end], 0, VIRUS_GENOME_SIZE - end);
             }