重複の可能性:
セットの疑似ランダムトラバーサル
プレイリストの曲をランダムな順序にするアルゴリズムを作成しようとしているので、10 曲ある場合は、繰り返す前に 0 から 9 までのすべての値をヒットする乱数ジェネレーターが必要です。アルゴリズムを使用して: 、 aおよびx_current = (a * x_prev + c) mod m
の特定の値でこれを達成する方法はありますか?c
m
重複の可能性:
セットの疑似ランダムトラバーサル
プレイリストの曲をランダムな順序にするアルゴリズムを作成しようとしているので、10 曲ある場合は、繰り返す前に 0 から 9 までのすべての値をヒットする乱数ジェネレーターが必要です。アルゴリズムを使用して: 、 aおよびx_current = (a * x_prev + c) mod m
の特定の値でこれを達成する方法はありますか?c
m
使ってみて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]);
この質問を見てください。また、小さなプレイリストの場合は、配列を曲番号でシャッフルするだけで十分です。