diff options
author | Jonathan Marshall <jmarshall@never.you.mind> | 2013-01-08 19:44:28 +1300 |
---|---|---|
committer | S. Davilla <davilla@4pi.com> | 2013-02-19 11:36:47 -0500 |
commit | 74f14b7b44b5bb515a7c0ecbf4c2d1dbe2e36836 (patch) | |
tree | 806e33c94eb40cca5fa9ee5d126847365d28e03f | |
parent | 4ea5cf9ce2451209b85dd485b652ca0022160310 (diff) |
don't use rand() for choosing a random song for partymode
-rw-r--r-- | xbmc/PartyModeManager.cpp | 10 | ||||
-rw-r--r-- | xbmc/music/MusicDatabase.cpp | 9 | ||||
-rw-r--r-- | 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<int,int> >& in, unsigned int number, vector< pair<int,int> >& 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())) |