How do I prevent a random value from repeating?

It’s simple, im making a random value 1-4 and let’s say the player rolled the action. It hits 3 then next time he does it he gets 3 again. I want them to get 1,2 but not 3 again 4. Like first they roll the decide it gets 3 and again they do they get 3 but this time i want the game to choose the other numbers 1, 2 and 4. So how do i? This is my current code it’s a mess but take a look. Etc for other numbers

1st script

2nd script

I believe it will be necessary to store the last number and, if it is the one drawn, draw another until it is not that one.

Hey Da-Just!
Recently, I had to use this system in my Color Bounce Template, but with Colors instead of numbers! :smile:

Let me explain how to do it:
First, download the “Array Tools” extension.
Create an array called “Numbers”. Make the children of it “1”, “2”, “3”, and “4”.
Follow the screenshots below:

(Run this event when you want to generate a new number)

To explain what happens, the Numbers array makes all puts all 4 of those numbers in a “list”. The code runs through the list each time until it reaches the bottom, the randomizes it and starts from the top again.

If there is any part of it that you don’t understand, or if I misunderstood the question, let me know! :smile:

1 Like

I love your minimalistic approach. I love lean events. Array tools is extremely useful.

As is, the first 4 items would be in the sequence of the array. You could add a shuffle to an at the beginning event or move the shuffle to an event before the number is picked.

If Count = 0 then shuffle

RandomNumber = numbers[count]
Count +1
Count = number of children in NUMBERS then set count to 0

You could also use mod() instead of adding 1 to make the number loop automatically but it’s early and mod() takes time to explain and I’m never sure which number to use. Number of children or children +/- 1. Again, it’s early.

1 Like

This works but it ain’t truly random i’m goin for

What are you going for? Random without repeating until every number is used or just not in a row or something else.

1 Like

Random like this. 1st the player gets num 4 then let’s say they get roll again and they ger num. 3rd time they roll the dice they shouldn’t be getting 4, but should be getting between 1 and 3. Since we started with 4 then went to 2 it should be between 1 and 3. Also going from 1 2 3 4 that’s not random but we’re +1 eveytime

Can you post a screenshot of your events. It seems like SnowyRawrGamer events should do that except, to be truly random, it needs to shuffle before the 1st play. Either through an at the beginning or the version that I posted.

I’m using the events from the SnowyRawrGamer showed me. Also here’s a vid link wut i’m tryin to do Watch record_2026-01-31_23-49-14 | Streamable I couldn’t understand your adds on, on the first comment you made too.

These are the changes that I was suggesting but I realize there is a flaw in the concept. I used different variable names, a button to trigger it and a text object to display the result.

This will shuffle the array and go through it without repeating. The issue is that when it’s reshuffled, the item that was the last item in the array could become the 1st item. So, it could repeat.

I don’t fully understand your setup and I unfortunately don’t have the free time at the moment.

One solution would be to remove the last picked item, shuffle and then add the item back either as the first item or the last item. If it was the last item then the index would have to be reduced instead of increased. Yeah, I’m confusing myself.

You need a way to shuffle the list while making sure the last item doesn’t become the first item. Although, you want to make it possible for the item to be the 2nd choice.

True randomness can be a complicated concept.

I’m hoping someone else has the time to fix the concept.

There’s another option that would use the object themselves with an object variable boolean to filter out the selected object so the pick random condition would only choose from the objects with a boolean value of false. The selected object would be set to true.

My concept isn’t fully thought out. Sorry.

1 Like

It seems like there should be an easier method but this is what I came up with.

It stores and removes the last item, so the next time it’s not in the array. If it’s not “” it puts it back the next time.

I used a text object to display the picks just to make sure it wasn’t repeating the same item in a row.

I create 3 text objects and put them into a group name Items. I used the RandomItem value to pick the text object with the same text. This could just as easily be an object variable like an ID.

It doesn’t shuffle the array, so it doesn’t use the array tools.

Thanks! I went with another way. And works well too as i intend :grin:

1 Like

I always just used a while statement for this.

This was for random state choices, and I’d set “LastLevel” when the scene started.

But you’d likely just be able to combine this logic with the array of objects like Keith had to do it in roughly the same or less number of events.

1 Like

I’ve never been a fan of the “brute force” method where it continues to create a random number. There’s too much unpredictability for me.

It did remind me of another method. You use mod().Think of it like your walking around in a pie divided by the number of items. Each time you take a random number of steps from 1 to the number of items minus 1. If it was the number of items you might end up where you started. So, you use minus 1. Then mod() will make the number wrap.

My results

Oh, same for me brute force alwats leaves distasteful in your mouth. However i made something else where it does samething :mending_heart::mending_heart:

Could you post a screenshot. I’m curious, plus it will help others.

1 Like

Oh it’s nothing like you imagine, i made a custom api to help with this, it basically does what i needed. Here’s the screenshot

1 Like

Two vars
Random number name
last random number name,
if they’re the same, redo the random number