Randomly choose an element in a list and remove the chosen one for next roundHi 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.
Automation and ScriptingRe: Randomly choose an element in a list and remove the chosen one for next roundOne 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] || ".");Re: Randomly choose an element in a list and remove the chosen one for next roundYou 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);
Re: Randomly choose an element in a list and remove the chosen one for next roundWould 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] || ".");