Removing a potential number from a Random selection

Hey, basically, I am building a trivia app and I want to know an efficient way to stop a number repeating itself from a random selection. e.g. Random number is rolled between 0 and 49 > the assigned questions/answers to that number loads on the screen > after the question is either skipped or answered the cycle begins again. How can I prevent the same number from being chosen?

I’ve tried to find some clues on how to do this online but the information wasn’t particularly helpful. I appreciate any attempts!

Thank you!

You’ll want to add each question to an array once it’s been chosen, then when generating a random question to display, add a ‘while’ loop to make sure the random question isn’t in the ‘chosen’ array.

Something along those lines anyway.

Hi Ruskimedveyt, for more info read everything that E1e5en said in the thread below and also look at their dice roll example. I’m a bit fuzzy on the details now but it’s what helped me.

It seems like you have a lot of items, so:
After you’ve looked at the dice roll example:
It’s not going to make much sense now but instead of manually adding the items individually back to the array when there were none left like in the dice roll example, I did it kind of automatically. I had to add 0 by itself though and then did the rest in one block using Clamp. I guess you could do it that way in the beginning to add them too, I haven’t got past a small sample test of this yet. I made this auto part up myself so don’t know if it has any problems with a bigger sample which I will eventually have. Oh, and ignore my numbers etc in the condition, they were for something very specific.



Any chance you can show me an example of this?

I’ve looked at this extensively, I can’t figure it out, or your example. Yeah I have 50 variables, perhaps even more soon, so this solution needs to be elegant enough that it wont break.

Please note, I somehow manage to get things done, but I’m not an expert. Here’s my little guide to the events screenshot shown in that other thread. But all of this might be useless to you with your firestore thing, I don’t know.

ArraySelectIndex is a number variable
SelectValue is a number variable
ArrayValue is an array with your questions, there will be 50 items in it etc.

Append value
You’ve done some firestone document thing which I know nothing about. The answer in that other thread is base on putting your questions in an array with each question having its own index, which will be there automatically.

Put all the items in the array, only two are shown in the screenshot. But you would have already put them in your array in the scene variables editor.

Get random index
You have 50 items, so this is where you’ll randomly choose one of them. But you’re not actually choosing a number from that list though, you’re just picking a number and then that gets assigned to the list in the next step.
The variable ArraySelectIndex will be whatever number is picked. Let’s say it’s 18.

Get value from array
Assign whatever number was picked in the ArraySelectIndex variable to the SelectValue variable: 18
So, the player will be presented with question 19.

Remove element to index
We don’t want the player to get question 19 again.
Our question picker variable, ArraySelectIndex is currently 18. Let’s get rid of that question number from your questions array ArrayValue by removing whatever is at index 18.

Not shown, but it’s in the dice roll example
When there’s no questions left in the questions array because you’ve removed them all, then what happens? For the dice roll, the numbers were added back again but for your trivia game, you may have other plans. This is where I put things back by using clamp, so it may be irrelevant to you.

You could try to get this working with a small number of questions and then optimise it by experimenting or asking more questions here.

I looked at yours, and a bunch of other examples, I opened up a separate scene and experimented for HOURS lol but I eventually found this to work.



So yeah, used a “while” event. My only problem now is, when it gets to the last variable the game freezes! Another problem to solve :smiley:

Otherwise, no questions are repeated anymore.

1 Like