Commit 4c4dea77 authored by Marius Pa's avatar Marius Pa Committed by Albert Astals Cid
Browse files

Remove qrand and qsrand

Fix issue "Obsolete function 'qrand' called. It is recommended to use
'QRandomGenerator' instead.".

qsrand is not needed because QRandomGenerator is already randomly initialized
parent b4427cfc
Pipeline #258889 passed with stage
in 1 minute and 11 seconds
......@@ -32,6 +32,8 @@
#include <iostream>
#include <vector>
#include <QRandomGenerator>
#include "aisquare.h"
#include "coord.h"
#include "dimension.h"
......@@ -81,9 +83,8 @@ usi AiBoard::height() const {
Coord AiBoard::move() {
if (m_cleanBoard) {
qsrand(static_cast<int>(std::time(nullptr)));
usi randX = qrand()%(m_dimension->width()/3) + m_dimension->width()/3;
usi randY = qrand()%(m_dimension->height()/3) + m_dimension->height()/3;
usi randX = QRandomGenerator::global()->bounded(m_dimension->width()/3) + m_dimension->width()/3;
usi randY = QRandomGenerator::global()->bounded(m_dimension->height()/3) + m_dimension->height()/3;
return Coord(randX, randY);
}
for (usi x = 0; x < m_dimension->width(); ++x) {
......
......@@ -28,14 +28,14 @@
#include <ctime>
#include <memory.h>
#include <QRandomGenerator>
// hash table
static NodeHashData hashData[nodeHashSize];
const int hashMaxDepth = 8;
const int hashMinRemainingDepth = 2;
bool rand_inited = false;
AiImpl::AiImpl()
: table_size_x(20)
, table_size_y(20)
......@@ -49,10 +49,6 @@ AiImpl::AiImpl()
, timeOver(nullptr)
, rememberedStanding(table_size_x, table_size_y)
{
if (!rand_inited) {
rand_inited = true;
qsrand(static_cast<unsigned int>(std::time(nullptr)));
}
memset(hashData, 0, sizeof(hashData));
}
......@@ -265,8 +261,8 @@ Field AiImpl::openingBook()
pos_T x, y;
x = table_size_x / 2;
y = table_size_y / 2;
x += qrand() % 5 - 2;
y += qrand() % 5 - 2;
x += QRandomGenerator::global()->bounded(5) - 2;
y += QRandomGenerator::global()->bounded(5) - 2;
while (rememberedStanding.table[x][y])
x++;
return {x, y};
......@@ -274,7 +270,7 @@ Field AiImpl::openingBook()
pos_T x, y;
x = rememberedStanding.lastx;
y = rememberedStanding.lasty;
int r = qrand() % 100;
int r = QRandomGenerator::global()->bounded(100);
if (r >= 20) {
if (x < table_size_x / 2) {
x++;
......@@ -304,19 +300,21 @@ Field AiImpl::openingBook()
dy = (int)y1 - (int)y2;
if (-1 <= dx && dx <= 1 && -1 <= dy && dy <= 1) {
if (dx == 0) {
return {static_cast<pos_T>((int)x1 + (qrand() % 2) * 2 - 1), static_cast<pos_T>((int)y1 + qrand() % 3 - 1)};
return {static_cast<pos_T>((int)x1 + (QRandomGenerator::global()->bounded(2)) * 2 - 1),
static_cast<pos_T>((int)y1 + QRandomGenerator::global()->bounded(3) - 1)};
}
if (dy == 0) {
return {static_cast<pos_T>((int)x1 + qrand() % 3 - 1), static_cast<pos_T>((int)y1 + (qrand() % 2) * 2 - 1)};
return {static_cast<pos_T>((int)x1 + QRandomGenerator::global()->bounded(3) - 1),
static_cast<pos_T>((int)y1 + (QRandomGenerator::global()->bounded(2)) * 2 - 1)};
}
if (qrand() % 2) {
if (qrand() % 2) {
if (QRandomGenerator::global()->bounded(2)) {
if (QRandomGenerator::global()->bounded(2)) {
return {static_cast<pos_T>((int)x1 + dx), y1};
} else {
return {x1, static_cast<pos_T>((int)y1 + dy)};
}
} else {
if (qrand() % 2) {
if (QRandomGenerator::global()->bounded(2)) {
return {static_cast<pos_T>((int)x1 - dx), static_cast<pos_T>((int)y1 + dy)};
} else {
return {static_cast<pos_T>((int)x1 + dx), static_cast<pos_T>((int)y1 - dy)};
......
......@@ -21,6 +21,7 @@
#include "standing.h"
#include <QRandomGenerator>
#include <QString>
#include <cassert>
#include <cstdlib>
......@@ -284,7 +285,7 @@ void Standing::evaluate()
decide();
if (current)
hval *= -1;
int current_seed = qrand() % (2 * (int)heur_seed + 1) - (int)heur_seed;
int current_seed = QRandomGenerator::global()->bounded(2 * (int)heur_seed + 1) - (int)heur_seed;
int hval_int = (int)hval + current_seed;
hval = hval_int > MaxHeur ? MaxHeur : hval_int < MinHeur ? MinHeur : hval_int;
}
......
......@@ -24,6 +24,7 @@
#include <QTimer>
#include <QPainter>
#include <QSvgRenderer>
#include <QRandomGenerator>
#include "common.h"
#include "coord.h"
......@@ -38,7 +39,7 @@ HintItem::HintItem(Scene* scene, const Move& hint, bool animate, qreal fill)
: QGraphicsSvgItem(), m_scene(scene), m_row(hint.y()),
m_col(hint.x()), m_fill(fill) {
setElementId(QString(hint.player() == X ? QStringLiteral("x%1") : QStringLiteral("o%1"))
.arg(QString::number(qrand() % 5 + 1)));
.arg(QString::number(QRandomGenerator::global()->bounded(5) + 1)));
m_tick = 16;
m_tickUp = true;
m_ticker = nullptr;
......
......@@ -24,6 +24,7 @@
#include <QTimer>
#include <QPainter>
#include <QSvgRenderer>
#include <QRandomGenerator>
#include "common.h"
#include "coord.h"
......@@ -37,7 +38,7 @@ namespace gui {
Mark::Mark(Scene* scene, const Move& move, bool animate, qreal fill) : QGraphicsSvgItem(),
m_scene(scene), m_row(move.y()), m_col(move.x()), m_fill(fill) {
setElementId(QString(move.player() == X ? QStringLiteral("x%1") : QStringLiteral("o%1"))
.arg(QString::number(qrand() % 5 + 1)));
.arg(QString::number(QRandomGenerator::global()->bounded(5) + 1)));
m_tick = 20;
m_ticker = nullptr;
if (animate) {
......
......@@ -51,7 +51,6 @@ Scene::Scene(const Theme& theme, bool animation)
/** @todo read file names from from some configuration, I guess */
m_renderer = nullptr;
loadTheme(theme);
qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
m_hintTimer = new QTimer(this);
m_hintTimer->setSingleShot(true);
m_hintItem = nullptr;
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment