From 74f14b7b44b5bb515a7c0ecbf4c2d1dbe2e36836 Mon Sep 17 00:00:00 2001 From: Jonathan Marshall Date: Tue, 8 Jan 2013 19:44:28 +1300 Subject: don't use rand() for choosing a random song for partymode --- xbmc/PartyModeManager.cpp | 10 +++------- xbmc/music/MusicDatabase.cpp | 9 +-------- xbmc/video/VideoDatabase.cpp | 7 +------ 3 files changed, 5 insertions(+), 21 deletions(-) diff --git a/xbmc/PartyModeManager.cpp b/xbmc/PartyModeManager.cpp index 1f5fc3e724..e301aac959 100644 --- a/xbmc/PartyModeManager.cpp +++ b/xbmc/PartyModeManager.cpp @@ -684,13 +684,9 @@ void CPartyModeManager::AddToHistory(int type, int songID) void CPartyModeManager::GetRandomSelection(vector< pair >& in, unsigned int number, vector< pair >& out) { - // only works if we have < 32768 in the in vector - for (unsigned int i = 0; i < number; i++) - { - int num = rand() % in.size(); - out.push_back(in[num]); - in.erase(in.begin() + num); - } + number = min(number, (unsigned int)in.size()); + random_shuffle(in.begin(), in.end()); + out.assign(in.begin(), in.begin() + number); } bool CPartyModeManager::IsEnabled(PartyModeContext context /* = PARTYMODECONTEXT_UNKNOWN */) const diff --git a/xbmc/music/MusicDatabase.cpp b/xbmc/music/MusicDatabase.cpp index bdee61aca8..faeaf7b4a5 100644 --- a/xbmc/music/MusicDatabase.cpp +++ b/xbmc/music/MusicDatabase.cpp @@ -4003,25 +4003,18 @@ bool CMusicDatabase::GetRandomSong(CFileItem* item, int& idSong, const Filter &f { idSong = -1; - int iCount = GetSongsCount(filter); - if (iCount <= 0) - return false; - int iRandom = rand() % iCount; - if (NULL == m_pDB.get()) return false; if (NULL == m_pDS.get()) return false; // We don't use PrepareSQL here, as the WHERE clause is already formatted CStdString strSQL = PrepareSQL("select %s from songview ", !filter.fields.empty() ? filter.fields.c_str() : "*"); Filter extFilter = filter; - extFilter.AppendOrder("idSong"); + extFilter.AppendOrder(PrepareSQL("RANDOM()")); extFilter.limit = "1"; if (!CDatabase::BuildSQL(strSQL, extFilter, strSQL)) return false; - strSQL += PrepareSQL(" OFFSET %i", iRandom); - CLog::Log(LOGDEBUG, "%s query = %s", __FUNCTION__, strSQL.c_str()); // run query if (!m_pDS->query(strSQL.c_str())) diff --git a/xbmc/video/VideoDatabase.cpp b/xbmc/video/VideoDatabase.cpp index f166a12444..ad99de4452 100644 --- a/xbmc/video/VideoDatabase.cpp +++ b/xbmc/video/VideoDatabase.cpp @@ -7214,17 +7214,12 @@ bool CVideoDatabase::GetRandomMusicVideo(CFileItem* item, int& idSong, const CSt { idSong = -1; - int iCount = GetMusicVideoCount(strWhere); - if (iCount <= 0) - return false; - int iRandom = rand() % iCount; - if (NULL == m_pDB.get()) return false; if (NULL == m_pDS.get()) return false; // We don't use PrepareSQL here, as the WHERE clause is already formatted. CStdString strSQL; - strSQL.Format("select * from musicvideoview where %s order by idMVideo limit 1 offset %i",strWhere.c_str(),iRandom); + strSQL.Format("select * from musicvideoview where %s order by RANDOM() limit 1",strWhere.c_str()); CLog::Log(LOGDEBUG, "%s query = %s", __FUNCTION__, strSQL.c_str()); // run query if (!m_pDS->query(strSQL.c_str())) -- cgit v1.2.3