cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Choose Language Hide Translation Bar
midori555
Level II

Randomly choose an element in a list and remove the chosen one for next round

Hi all,

I have a list[1,2,3,4,5,6,7,8,9,10] that I want to randomly choose an element, and then remove that element from the list for the next random selection. I need to repeat the selections 4 times.

Is there a easy way to remove the selected element in the list? Both Remove and Remove From need the ith index of the elements.

 

Thank you for your help.

 

2 ACCEPTED SOLUTIONS

Accepted Solutions
Jeff_Perkinson
Community Manager Community Manager

Re: Randomly choose an element in a list and remove the chosen one for next round

Would something like this work?

 

theList = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j"};

while(nitems(theList) > 1, 
r=random integer(nitems(theList));

print("The chosen item was " || theList[r] || ". Element number " || char(r) || ".");

remove from(theList, r);
);

print("The chosen item is " || theList[1] || ".");
-Jeff

View solution in original post

jthi
Super User

Re: Randomly choose an element in a list and remove the chosen one for next round

You could also use Random Shuffle to randomize the list (matrix) and then loop over the randomized list (matrix) four times or pick first four elements

Names Default To Here(1);

m = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
m_rand = Random Shuffle(m);
Show(m, m_rand);
-Jarmo

View solution in original post

3 REPLIES 3
Jeff_Perkinson
Community Manager Community Manager

Re: Randomly choose an element in a list and remove the chosen one for next round

Would something like this work?

 

theList = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j"};

while(nitems(theList) > 1, 
r=random integer(nitems(theList));

print("The chosen item was " || theList[r] || ". Element number " || char(r) || ".");

remove from(theList, r);
);

print("The chosen item is " || theList[1] || ".");
-Jeff
jthi
Super User

Re: Randomly choose an element in a list and remove the chosen one for next round

One edited version of @Jeff_Perkinson version, which uses the Remove From return value (it returns the value of removed elements as list). This code might not be as easy to read, but sometimes using the return value from Remove From is useful:

Names Default To Here(1);

theList = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j"};

While(N Items(theList) > 1,
	// returns list of removed elements, we remove one value, so get first index
	r_val = Remove From(theList, Random Integer(N Items(theList)))[1]; 
	Print(Eval Insert("The chosen item was ^r_val^."));
);

Print("The chosen item is " || theList[1] || ".");
-Jarmo
jthi
Super User

Re: Randomly choose an element in a list and remove the chosen one for next round

You could also use Random Shuffle to randomize the list (matrix) and then loop over the randomized list (matrix) four times or pick first four elements

Names Default To Here(1);

m = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
m_rand = Random Shuffle(m);
Show(m, m_rand);
-Jarmo