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.
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.
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
We fill the array in the editor. As an example, I showed how an array can be supplemented with values.
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.
Get the value from the array at the ArraySelectIndex index and write the value to the SelectValue variable.
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)
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.
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)