I have an array in which i want to select randomly from 5 separate times with no repeat selections. The part that is puzzling in the editor is how to make it so the picks are at random, but still have it so there are no duplicate selections. So for example, i have an array (size of 24) and i am selecting a random integer, and then picking from the array (5 separate times):
So if i run that 5 times, obviously there is a high probability of duplicate selections from the array (a random integer is able to be the same each time it loops). Is there a way to set exclusions each time it loops so there is no repeat selections? In standard programming there are ways to shuffle arrays and such.. but i'm completely stumped in this editor.
Add each used random to an array, then every time you get a new random, check if it's in that array, and if it is, then re-roll the random.
Pseudo code:
...
Repeat
..isUnique = true;
..Random Picker = (Random integer between 0 and 23);
..For i from 0 to 4 Do
....If (Random Picker == usedRands[i]) Then isUnique = false;
Until(isUnique);
Unit - Create ...
It's the simplified version. You could use functions to get rid of For loop inside the repeat.
Update: GOD DAMN IT, I hate this markup, it has removed all my spaces, had to write them with dots.
While(Qty Array[r] == -1) Let's say you don't use -1 in your array
r = Random Integer 0 - 23
Create unit Qty Array[r]...
set Qty Array[r] = -1
Just make sure you always "remove" this way without exceeding array size. Otherwise all elements will be -1 and while will never end. If you want your array to keep values after that you need some separate action which will repopulate array
With extremely bad luck or big array size it may slightly lag tho. For this i may recommend my List <T> which may easily remove value from List at given index and return new items count
With list you would do
You could also replace the random array slot with your last one and decrease count by one, if the order doesn't matter, or use 2 arrays in conjunction (which would essentially be the same as using a list ;))
I have an array in which i want to select randomly from 5 separate times with no repeat selections. The part that is puzzling in the editor is how to make it so the picks are at random, but still have it so there are no duplicate selections. So for example, i have an array (size of 24) and i am selecting a random integer, and then picking from the array (5 separate times):
So if i run that 5 times, obviously there is a high probability of duplicate selections from the array (a random integer is able to be the same each time it loops). Is there a way to set exclusions each time it loops so there is no repeat selections? In standard programming there are ways to shuffle arrays and such.. but i'm completely stumped in this editor.
Thanks for the help in advance
Add each used random to an array, then every time you get a new random, check if it's in that array, and if it is, then re-roll the random.
Pseudo code:
...
Repeat
..isUnique = true;
..Random Picker = (Random integer between 0 and 23);
..For i from 0 to 4 Do
....If (Random Picker == usedRands[i]) Then isUnique = false;
Until(isUnique);
Unit - Create ...
It's the simplified version. You could use functions to get rid of For loop inside the repeat.
Update: GOD DAMN IT, I hate this markup, it has removed all my spaces, had to write them with dots.
easy
Just make sure you always "remove" this way without exceeding array size. Otherwise all elements will be -1 and while will never end. If you want your array to keep values after that you need some separate action which will repopulate array
With extremely bad luck or big array size it may slightly lag tho. For this i may recommend my List <T> which may easily remove value from List at given index and return new items count
With list you would do
You could also replace the random array slot with your last one and decrease count by one, if the order doesn't matter, or use 2 arrays in conjunction (which would essentially be the same as using a list ;))
This way, you won't have any loops and (negligible) bad luck performance spikes, but an incomplete array after use.
Long story short you guys are smart as hell.
Thanks for your help. As usual.