Skip to content

Commit

Permalink
Fix random number selection issues on some OS
Browse files Browse the repository at this point in the history
  • Loading branch information
Isarhamster committed Jan 14, 2024
1 parent 3c6a446 commit 7dc27e3
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 4 deletions.
6 changes: 4 additions & 2 deletions src/gui/analysiswidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
#include "tablebase.h"

#include <QMutexLocker>
#include <QRandomGenerator>

#include <algorithm>

using namespace chessx;
Expand Down Expand Up @@ -443,12 +445,12 @@ void AnalysisWidget::sendBookMoveTimeout()
int index = 0;
if (m_moveTime.bookMove == 1)
{
index = rand() % moveList.count();
index = QRandomGenerator::global()->bounded(0,moveList.count()-1);
}
else if (m_moveTime.bookMove == 2)
{
index = moveList.count() - 1;
int randomPos = rand() % games;
int randomPos = QRandomGenerator::global()->bounded(0,games-1);
for (int i=0; i<moveList.count();++i)
{
randomPos -= moveList.at(i).results.count();
Expand Down
3 changes: 2 additions & 1 deletion src/gui/boardsetup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <QtGui>
#include <QPixmap>
#include <QPushButton>
#include <QRandomGenerator>
#include <QRegularExpression>
#include <QSpacerItem>
#include <QSizePolicy>
Expand Down Expand Up @@ -148,7 +149,7 @@ void BoardSetupDialog::chess960posChanged(int value)

void BoardSetupDialog::chess960randomPos()
{
int randomPos = rand() % 960;
int randomPos = QRandomGenerator::global()->bounded(0,959);
ui.chess960pos->setValue(randomPos);
}

Expand Down
3 changes: 2 additions & 1 deletion src/gui/gamelist.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include <QHeaderView>
#include <QMenu>
#include <QPixmap>
#include <QRandomGenerator>

#if defined(_MSC_VER) && defined(_DEBUG)
#define DEBUG_NEW new( _NORMAL_BLOCK, __FILE__, __LINE__ )
Expand Down Expand Up @@ -262,7 +263,7 @@ void GameList::selectRandomGame()
{
QModelIndex sortIndex = currentIndex();
int oldRow = sortIndex.row();
int randomSortRow = rand() % (m_model->filter()->count()-1); // The last game is represented by current game
int randomSortRow = QRandomGenerator::global()->bounded(0,m_model->filter()->count()-2); // The last game is represented by current game
if (oldRow == randomSortRow)
{
randomSortRow = m_model->filter()->count()-1;
Expand Down

0 comments on commit 7dc27e3

Please sign in to comment.