Permalink
Browse files

don't use rand() for choosing a random song for partymode

  • Loading branch information...
1 parent ed80802 commit 345f737ad7a4484b3b03c668cfa1132173b907ef Jonathan Marshall committed Jan 8, 2013
Showing with 5 additions and 21 deletions.
  1. +3 −7 xbmc/PartyModeManager.cpp
  2. +1 −8 xbmc/music/MusicDatabase.cpp
  3. +1 −6 xbmc/video/VideoDatabase.cpp
View
@@ -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
@@ -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()))
@@ -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()))

0 comments on commit 345f737

Please sign in to comment.