How can i make an array pick random and unique elements?

I have an array of 7 strings, and i want to pick 5 of them that are all different from each other. How can i achieve that? I tried using for loop and while but i can’t figure it out. Even shuffle doesn’t seem to work.

1 Like

You just need to shuffle, and then loop from 1 to 5. What is not working out? :slight_smile:

I’d also add a contains test before adding to the result array. If the result array already contains the shuffled number, then shuffle until it’s unique.

Just because it’s random doesn’t mean that in 5 tries you can’t get a duplicate, unless you would work on a copy of the array and remove the random element each shuffle.

Sorry but i really cannot figure it out, where am i wrong?

So i’ll explain how i see this. I make an array, then shuffle it. After that i start a for loop from 0 to 4, and set a new array with a get connected to the index in order to get them in their respective index order after shuffle. Then i “convert it to an array?”, and add that to the new array. The final print is testing purposes, and obviously im wrong in this example because it’s not working. I think the problem is the get, which doesn’t seem to do exactly what i want, but i can’t seem to find any other node that works better.

1 Like

Make the array and put it in a variable. The way have it here, you’re making a array every time you pick anything from it :slight_smile:

So i need to add 1 variable per array pick?

1 Like

Make array → variable → shuffle → everything else :slight_smile:

At the moment, every time around the loop, this line gets called

and ‘make array’ is called.

That’s why you need to put the output of make array into a variable. Then just use the variable.

Like this? Then in the loop body what do i need to do?

1 Like

Correct :slight_smile:

In the loop use the variable you’ve made. Do the get from the variable.

Then what? :slight_smile:

Like this

I understand what youre saying but i want to save the picks in a new array

1 Like

Ok. This bit does nothing

You want to make an array of what was taken from the original?

I want to pick from the array of 7 elements, 5 random elements that are unique and add them to a new array

1 Like

It works for you? :smiley:

Yes, thanks!

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.