I can’t simply randomize the song list, cause the songs will repeat, and the scoring system is based on how many you got right in a row. there are 3 different questions per song that can’t be repeated, so for 8 songs, you have 24 scenarios. so how do i randomize it so the same questions for each song aren’t repeated til a new game? i’ve heard prng but have no idea how they work or how to write one ingame. example scripts plus a explanation of concept will help.
I dont know if i get you right, maybe you could put the questions in a List and once you chose one, remove it?
The key here is to “shuffle” rather than to “randomly select”.
E.g. If one shuffles a deck and then removes cards from it he is guaranteed not to ever get a duplicate card. He does however have an issue when he gets to the end of the deck.
If, alternatively, he randomly selects a card from the deck [without removing it] then he may select the same card twice!
On line 81 of Shuffler - Pastebin.com I have an example of a shuffle algorithm for lists.
http://mrmoss.net/2013/cunity3d-shuffling-an-array/
Can easily be converted to AudoClip array
public void ShuffleAudioClips(AudioClip[] obj)
{
for (int i = 0; i < obj.Length; i++)
{
AudioClip temp = obj[i];
int obj_index = Random.Range(0, obj.Length);
obj[i] = obj[obj_index];
obj[obj_index] = temp;
}
}
how does your script work, and is there a way i could reconstruct it with unityscript or c# ?
my script is C#… So is the post above me’s script.
Just simply call ShuffleAudioClips(yourarrayofclips); and they will be shuffled.
Is not a good shuffling algorithm. I just did some crude tests, and noticed some disturbing bias:
----------
Starting Test Orderby RNG -- Test Data Generated in 139ms
Finished Shuffing in 185ms!
1.20 %
99.93 %, 99.87 %, 100.17 %, 99.89 %, 99.85 %, 100.12 %, 99.93 %, 100.12 %, 100.19 %, 99.93 %,
Starting Test Orderby CRNG -- Test Data Generated in 101ms
Finished Shuffing in 4486ms!
1.39 %
99.80 %, 100.00 %, 99.99 %, 99.69 %, 99.94 %, 100.32 %, 100.17 %, 99.89 %, 100.12 %, 100.08 %,
Starting Test Shuffle RNG -- Test Data Generated in 102ms
Finished Shuffing in 79ms!
1.60 %
100.49 %, 100.06 %, 99.94 %, 100.02 %, 99.99 %, 99.84 %, 99.70 %, 99.92 %, 100.22 %, 99.81 %,
Starting Test Shuffle CRNG -- Test Data Generated in 99ms
Finished Shuffing in 3885ms!
1.23 %
99.93 %, 100.13 %, 100.00 %, 100.00 %, 100.31 %, 99.84 %, 99.72 %, 100.10 %, 99.90 %, 100.06 %,
Starting Test EliteMossy Shuffle -- Test Data Generated in 106ms
Finished Shuffing in 64ms!
25.46 %
93.12 %, 95.65 %, 98.50 %, 100.28 %, 101.61 %, 102.80 %, 103.12 %, 103.07 %, 101.66 %, 100.19 %,
List of 10 elements, 100,000 iterations. I report the deviation from perfect distribution [e.g. sum of element / sum of all elements * number of elements] - both as a sum of the deviation for all elements and each individual element.
I know it has flaws, but does not matter for non critical stuff like this.
But given that the fix is literally a single character change:
public void ShuffleAudioClips(AudioClip[] obj)
{
for (int i = 0; i < obj.Length; i++)
{
AudioClip temp = obj[i];
//int obj_index = Random.Range(0, obj.Length);
int obj_index = Random.Range(i, obj.Length);
obj[i] = obj[obj_index];
obj[obj_index] = temp;
}
}
It’s worth noting?
Yeah, definitely, thanks ![]()