No, that's not what I meant.
I meant that you create an array where you store the numbers of your items still available, and then when you want to choose a random item,
choose randomly from that array and take the number away. When doing this it is required that when number is taken away from array, the numbers after it are moved to fill the hole.
Example:
Items that can be selected randomly:
1 2 3 4 5 6
Game randomly selects number
3So now 3 can't be used again and removing it from the array results to this:
1 2 0 4 5 6
We can't have that 0 there, so we have to move the numbers after it, so:
1 2 4 5 6 0
And now the next time we choose a number we limit it to choose the number from the five first numbers, which means excluding the number 0.
Here's how I wrote it:
Global Code:
- Code: Select all
int selected;
int selectableItemsLeft = 6;
int selectableItems[6] = {1, 2, 3, 4, 5, 6};
These variables are needed. I think you already have the variable selected, because you used it in your code.
The integer selectableItemsLeft holds the amount of items not used, so that we know where we have to limit the GetRandomBetween maximum value.
And selectableItems[6] is the array that holds the numbers of the items possible to be selected.
And this code goes to where ever you want to select a random item, which has not been used yet:
- Code: Select all
int i, move = 0;
int selectRandom;
if (selectableItemsLeft > 0)
{
selectRandom = GetRandomBetween(0, selectableItemsLeft);
selected = selectableItems[selectRandom];
selectableItemsLeft -= 1;
for (i = 0; i <= 6; i ++)
{
if (i == selectRandom){move = 1; selectableItems[i] = 0;}
if (move == 1 && i < 5)
{
selectableItems[i] = selectableItems[i + 1];
selectableItems[i + 1] = 0;
}
}
}
The code explained:
Line number - explanation
1 - make local variables i and move. i is for the 'for'-loop, move for controlling when the numbers have to be shifted down (this has to be set to 0, or otherwise it'll shift on its own
).
2 - make local variable selectRandom, which is used for storing the randomly selected array index.
4 - check if there is still some unused items left
6 - set selectRandom to point to some of the numbers in the selectableItems[] -array. The min limit must be 0 so that we can get the first value too. (Array cell indexes start from 0)
7 - set selected to be the number in the array selectableItems[] in which the selectRandom points to.
9 - reduce one from selectableItemsLeft. We need to do this so that next time when GetRandomBetween() gets number from the array we don't select the 0 we are going to put to the end of it.
11 - start for loop for going through all the array cells of the array selectableItems[]
13 - if we are in the cell that contains the number which we just selected, set move to 1 (for knowing that now we need to shift down all the numbers to come) and replace the selected number with 0.
14 - if move is equal to 1 and i is less than 5 (i must be less than five because inside this if statement we are going to modify the array cell numbered with the value of i + 1, and if i = 5, we would be modifying array cell number 6, which doesn't exist, since our array has just 6 cells (indexing begins from 0). And doing that would result to an error.)
16 - copy the number from next array cell to the current one
17 - reset the next array cell
And now, it works!
Here's a demo, too. Read the instructions in the demo.
If you still don't understand this, just ask!