0

Possible Duplicate:
Pseudo-Random Traversal of a Set

I'm trying to write an algorithm that will put the songs of a playlist into a random order, so if there are 10 songs, I need the random number generator to hit every value from 0-9 before repeating. Using the algorithm: x_current = (a * x_prev + c) mod m, is there any way to achieve this with certain values for a c and m?

Community
  • 1
  • 1
kjh
  • 3,407
  • 8
  • 42
  • 79

2 Answers2

2

Try using std::random_shuffle

 vector<int> playOrder;    

  // set some values:
  for (int i=1; i<10; ++i) playOrder.push_back(i); // 1 2 3 4 5 6 7 8 9

  // Don't forget to seed, or mix will be the same each run
  srand(time(NULL)); 

  // using built-in random generator:
  random_shuffle ( playOrder.begin(), playOrder.end() );

  // An example of how you might use the new random array.
  for(int i=0; i<playOrder.size(); i++)
    player.PlayTrack(playOrder[i]);
Aesthete
  • 18,622
  • 6
  • 36
  • 45
0

Take a look at this question. Also, for small playlists, simply shuffling an array with the song numbers will be sufficient.

Community
  • 1
  • 1
Chris Hayes
  • 11,471
  • 4
  • 32
  • 47
  • "Simply shuffling an array" is the problem description. – Lightness Races in Orbit Nov 11 '12 at 01:35
  • Traversing a set in random order without repetition is the problem description. Shuffling an array would be a possible solution. – Chris Hayes Nov 11 '12 at 01:37
  • You are correct; I am simply suggesting that you may need to progress slightly further from the problem description in order for the OP to recognise it as a possible solution. :) His proposed "algorithm" is already intended to essentially shuffle an array and he is trying to find a different way to do it. – Lightness Races in Orbit Nov 11 '12 at 01:38