Download

How can I remove an Index from an Array that can't be accessed again?

I am trying to get a value for 5 different numbers. Each one has to be a random integer within a range. I need it setup so that I cannot get two of the same number so I made an array that has values from 1-59. I get a random index and that supplies me with a random number, but I can still draw the same number for the other values. So I have been tinkering around trying to figure out how to say “scratch” out an index so that I cannot access it again. I started using a remove index every time a value is called. I thought that would work but it doesn’t. With that method I am still running into duplicate numbers and once all indexes are removed it just starts getting a value of 0 even though I shouldn’t be able to go under 1. So I need to figure out a way in BP to have a set number of values and once a valued is called it cannot be called again.

That’s simple, make a 2nd array. This array will store indexes of the first array that have been called, and shouldn’t be able to be used again.

But quick question, you don’t want to be able to get the same value again, or the same index?

Here’s an example for not getting the same index twice:
Each time you get the value of an index from the 1st array, add a new item to the 2nd array that equals the index of 1st array. Here’s a working example:

9137d39a1ea578ef21a6a8e9531a0377b10fff93.png

This will basically do what you said and get a random index, and will check if that index has already been used, if it has, it gets another random index.There is still at 1 in ~3600 chance it will pick the same index twice in a row with 59*59.This eliminates the need for removing indexes.

If you want to, you can even ‘clear’ the 2nd index to let the indexes that have already been picked be picked again, or individually remove indexes.

If you want me to make one that checks for used value instead of already used index, give me a hollar.

Nice this could work. The value is really the most important thing. So if it gives me 40 then none of the other numbers can be 40. I do have a working solution for what I needed but I was hoping to learn some more about arrays. My solution was to not use an array at all. I just have 5 values, each are set with a random integer from 1-59 and then I have a ton of branches that check to see if the value matches any of the previous values and if it does subtract by 1 then repeat that until the number cannot be the same. It works with no chance of getting the same values. I just figured there might of been an easier way to achieve the same result with out having to do a bunch of branches checking for values and modifying. Thanks for your help!

Alright, yeah, no need for a bunch of branches, let me quickly modify my setup for duplicate values:

4adde9fc0af2b2136d236bb3da8271af36d0e6e4.png

This will not let the same value be picked twice, there is still 1/3600 chance it will be, but can easily be modified for 1/200,000 chance by adding a second select float.

1/3600 is still very precise, hope I helped :wink:

I am trying to implement this and I have a question. Can I get an understanding of why you are using a -1 integer for your condition after the FIND node?

Because what it does is it tries to find the index of the value, if it’s -1, that means the index doesn’t exist, which means the value hasn’t yet been added to the array. So if it’s -1, it uses the value because it isn’t “blacklisted” yet. And if it isn’t -1, that means it’s in the array because an index exists for it, therefore it should pick another random.

Ok I understand now. Thank you for your help! I will figure this out

No problem :slight_smile:

Personally I would just get the index of the selected 1 and remove it from the array which you can do with the “Remove Index” node.
Just on start of the game though create a replica array of the original state which once you want to start over with again set your main array to use it’s values. This way you have zero chance of getting the same number again.

While my method has a 1/3600 chance of same values in James’ case, my method also allows for fast value “re-using” (Let’s it be used again) by simply removing it from the 2nd index, and nothing more, while your method would require a check to see if it’s already there before removing it. Also if he has a pattern associated with his indexes & values, that pattern would become messed up when trying to re-add/reuse a value without storing it’s previous index or not being able to use a value of 0.

Nope there is no check required at all. You just spit the value out, and delete it from the array. Never have to do any check again on it because its gone from the array. You just recreate the array to it’s original state when you want to work with it again. This would be like having a deck of cards and removing 1 card from it. You wouldn’t want to clone your card and then have to check if it exists or not, you would take it out of the deck. Then when you start a new hand you reset the deck/array and deal again.

I had an issue when I was using the remove index node. I have it set to pull a random integer between 1-59 and after I move several indexes somehow I was ending up with values of 0 which I shouldn’t be able to get a 0 but I guess since it removes the index but then for instance it removed 58 and then I pulled 58 again its just 0.

Vigeo James, maybe instead of getting random between 1-59, get a value between 1-arrayLength?