Your request seems slightly contradictory: when the random choice selects a
you want it to recurse, but if it chooses a number you still want to continue until there are no a
's left.
The following code does this, but the recursion is technically unnecessary. Perhaps in your application it will be applicable.
The first part shows how you can recurse selections of a
by using Hold
:-
Clear[a]
list = {1, 2, a, 1, a, 3, a, 1, 4, 5};
heldlist = Hold /@ list;
a := Module[{z}, z = RandomChoice[heldlist];
Print["for information, choice was ", z];
If[MatchQ[z, Hold[_Symbol]],
heldlist = DeleteCases[heldlist, z, 1, 1];
If[MemberQ[heldlist, Hold[_Symbol]], ReleaseHold[z]]]]
a
On this occasion, calling a
recurses once, then picks a 4 and stops, as expected.
for information, choice was Hold[a]
for information, choice was Hold[4]
To make the process continue until there are no a
's While
can be used. There is recursion going on here too but While
keeps the process going when a number is picked.
While[MemberQ[heldlist, Hold[_Symbol]], a]
for information, choice was Hold[a]
for information, choice was Hold[1]
for information, choice was Hold[a]
These are the remaining items in the list :-
ReleaseHold /@ heldlist
{1, 2, 1, 3, 1, 4, 5}