Manipulate a list: replace random position of a specific integer n times by 0
MapAt[0 &, #, RandomSample[Position[#, 1], UpTo@3]] & /@ d
{{0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 1}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}}
Module[{s= SparseArray[RandomSample[Position[#, 1], UpTo@3] -> 0, Length@#, 1]}, s #] & /@ d
{{0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0}, {0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 1}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}}
SubsetMap[{0, 0, 0} &, #, RandomSample[Position[#, 1], UpTo@3]] & /@ d
{{1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0}, {0, 1, 0, 1, 0, 0, 1, 0, 0, 1, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}}
A function that takes a list and replaces a random 1 by a 0:
replaceone[L_] := ReplacePart[L, RandomChoice[Position[L, 1]][[1]] -> 0]
Apply it three times to all sublists of d
:
Nest[replaceone, #, 3] & /@ d
(* {{0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0},
{0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 1},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}} *)
Use ReplacePart
:
d = {{1, 1, 0, 1, 1, 1, 0, 0, 1, 0, 0}, {0, 1, 1, 1, 0, 0, 1, 0, 1, 1,
1}, {0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0}};
f = ReplacePart[#, RandomSample[Position[#, 1], 3] -> 0]&
Print[f /@ d]
(* {{1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0}, {0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}} *)
Try it online!
If you want it to work on lists that contain fewer than 3 1s, replacing them all with 0s, use UpTo[3]
instead of 3
.