 # Remove numbers from a randomised range

Hi, how to remove numbers from a range as they are drawn?

Example: RandomInRange(1,5)
First draw = 3 (removed from the next range)
Secund draw will be between numbers: 1,2,4,5. Secund draw = 4
Third draw will be between numbers: 1,2,5. Third draw = 1
Forth draw will be between numbers: 2,5. Third draw = 5
Last draw will give the remaining number: 2.

My range has more than five values so I would like to automate this operation.

You can use a temp array variable then remove the key from the array when the number match

Hi. Use an array to get a random value. Example, array [1, 2, 3, 4, 5]. Get the value from it through Randow ([the length of the array]) and remove the element. To work with an array, you can use the Array Tools extension.

1 Like

Use an array.

Steps:

1. Put all the number into the array.
2. Randomise them within the array.
3. Read the number from position 0, and remove it. The other numbers will shift to fill the hole.
4. Repeat step 3 until the array is empty.

 @E1e5en’s solution seems slightly simpler than mine 2 Likes

You can mix the array using an extension, there is such a function, but there are restrictions on use (** only works on root scene variables **). Therefore, you can get and remove an element by the usual means.

@MrMen @UlisesFreitas @E1e5en
Thanks guys. Your level is higher than mine.
I just installed the array tools extension and I try to understand how it works… I try to understand what you mean as well Example, without using an extension:

Later, I will make an example using the extension
Update: Corrected the mistake

Sorry, I made a mistake on this line:

The algorithm for my example is as follows:

1. We fill the array in the editor. As an example, I showed how an array can be supplemented with values.
2. Get a random index into the array using the function `Random ([array length])`, in this case 5 (total of 6 elements with indices from 0 to 5). Save the result to the `ArraySelectIndex` variable.
3. Get the value from the array at the `ArraySelectIndex` index and write the value to the `SelectValue` variable.
4. Remove this value from the array at the `ArraySelectIndex` index. Thus, on the next execution from step 2 to 4, we will get a different value from the array. Since we removed this.

Do not forget that in step 2, with each iteration, you must specify the correct length of the array and make a check that the elements in the array remain. Otherwise, you will get an error when trying to get a value that does not exist.

Another way around is:
Using a dummy object, little and not visible.
put a scene variable the lower value of your range (ex. 1) and with a repeat cycle make objects up to your upper limit (ex 6), when you create them, test if the value of a variable is 0, if it is, change it into the scene variable value. After it, add 1 to the scene variable. Now you have your array of numbers.

With the array ready, use the “pick a random object” event. Take the object varaible value (Your random number), and delete the object after it (So you will not repeat the value)

I used that kind of events for the jigsaw puzzle for my game, to present the pieces in a mixed way.

Project example: dice_roll.json

In this case, I use X=0 and Y=1 as a starting point. So my random range goes from 1 to 16. The line (Varaible(X)*4)+Variable(Y) does that trick (I was putting the dummy objects inside a box and then covering them with the jigsaw pieces).

When I get all the numbers, I change the pieces’ animation frame to the value of Pieza.Variable(ID)-1 and put the animation speed to 0.

@oscuridad666 in terms of resources, this is very wasteful.

Not many objects, the work is done, plus I use them for the jigsaw answer logic (getting them in place and comparing the ID of both the dummy and the piece, when all of them are matching, the jigsaw is complete)

If you need these objects, then this is one thing. But to get all numbers from the specified sequence in a random order is not optimal.

Yes, totally agree. I can’t imagine doing it for more than 100 objects, (even with little, not mobile, not visible objects).

In my case, I am using just 16 objects for a simple jigsaw (4*4)