Manipulate a list: replace random position of a specific integer n times by 0Avoid making repeated Random Integer in different iterationsRandom Integer SequencesModifying the elements of a list according to a given probabilityhow to update elements in a matrix with position identified in a listHow to Floor specific entries of list within a matrix?Replacing elements of a list using a pure function fails; why?List manipulation with random elements under limiting conditionsHow to split sublists into sub-sublists without merging the sublistsReplace single element of a nested list with multiple elementsColoring elements in nested list consistently over all sublists

Polyhedra, Polyhedron, Polytopes and Polygon

Why did Windows 95 crash the whole system but newer Windows only crashed programs?

How to store my pliers and wire cutters on my desk?

Compound Word Neologism

Why didn’t Christianity spread southwards from Ethiopia in the Middle Ages?

Are the named pipe created by `mknod` and the FIFO created by `mkfifo` equivalent?

How to efficiently shred a lot of cabbage?

Dobbs Murder Mystery : A Picture worth 1000 words?

Japanese reading of an integer

Why does the Eurostar not show youth pricing?

Does Dispel Magic destroy Artificer Turrets?

How can Paypal know my card is being used in another account?

Is it okay for me to decline a project on ethical grounds?

Why is it considered acid rain with pH <5.6?

2 weeks and a tight budget to prepare for Z-day. How long can I hunker down?

Self-deportation of American Citizens from US

Assuring luggage isn't lost with short layover

Incrementing add under condition in pandas

What container to use to store developer concentrate?

How did the Axis intend to hold the Caucasus?

Does Wolfram Mathworld make a mistake describing a discrete probability distribution with a probability density function?

Do the books ever say oliphaunts aren’t elephants?

What steps would an amateur scientist have to take in order to get a scientific breakthrough published?

What would the United Kingdom's "optimal" Brexit deal look like?



Manipulate a list: replace random position of a specific integer n times by 0


Avoid making repeated Random Integer in different iterationsRandom Integer SequencesModifying the elements of a list according to a given probabilityhow to update elements in a matrix with position identified in a listHow to Floor specific entries of list within a matrix?Replacing elements of a list using a pure function fails; why?List manipulation with random elements under limiting conditionsHow to split sublists into sub-sublists without merging the sublistsReplace single element of a nested list with multiple elementsColoring elements in nested list consistently over all sublists






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








5












$begingroup$


I have this matrix with 3 sublists:



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;


For learning purposes, I try to replace integer 1 (randomly 3 times) in each sublist with 0.



The output can be like this after replace number 1 in a random position of each sublist by 0 tree times:



new d = 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0


Any help appreciated.










share|improve this question











$endgroup$




















    5












    $begingroup$


    I have this matrix with 3 sublists:



    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;


    For learning purposes, I try to replace integer 1 (randomly 3 times) in each sublist with 0.



    The output can be like this after replace number 1 in a random position of each sublist by 0 tree times:



    new d = 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0,
    1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0


    Any help appreciated.










    share|improve this question











    $endgroup$
















      5












      5








      5





      $begingroup$


      I have this matrix with 3 sublists:



      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;


      For learning purposes, I try to replace integer 1 (randomly 3 times) in each sublist with 0.



      The output can be like this after replace number 1 in a random position of each sublist by 0 tree times:



      new d = 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0,
      1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0


      Any help appreciated.










      share|improve this question











      $endgroup$




      I have this matrix with 3 sublists:



      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;


      For learning purposes, I try to replace integer 1 (randomly 3 times) in each sublist with 0.



      The output can be like this after replace number 1 in a random position of each sublist by 0 tree times:



      new d = 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0,
      1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0


      Any help appreciated.







      list-manipulation matrix random






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jul 19 at 6:38









      user64494

      4,1562 gold badges14 silver badges23 bronze badges




      4,1562 gold badges14 silver badges23 bronze badges










      asked Jul 18 at 21:03









      Ben AawfBen Aawf

      908 bronze badges




      908 bronze badges























          4 Answers
          4






          active

          oldest

          votes


















          3












          $begingroup$

          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.






          share|improve this answer









          $endgroup$














          • $begingroup$
            That's great.#lirtosiast. One question more, please. How I can use If function. I want to pick randomly 3 elements from each sublist in matrix d and replace by if 0->1, or 1->0
            $endgroup$
            – Ben Aawf
            Jul 18 at 21:39











          • $begingroup$
            @BenAawf MapAt (used in klgr's answer) is more general than ReplacePart; you can do for instance f = MapAt[1-#&, #, RandomSample[Position[#, 0 | 1], 3]]&, which does your replacement and leaves all elements not 0 or 1 unchanged.
            $endgroup$
            – lirtosiast
            Jul 18 at 21:43


















          5












          $begingroup$

          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







          share|improve this answer











          $endgroup$














          • $begingroup$
            wooow that's so helpful thank you so much @kglr
            $endgroup$
            – Ben Aawf
            Jul 18 at 22:06


















          3












          $begingroup$

          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 *)





          share|improve this answer











          $endgroup$






















            1












            $begingroup$

            I guess I'll throw my hat in the ring.



            f[list_, numtorep_, numofreps_] := Block[
            data = list,
            Apply[
            (data[[##]] = 0) &,
            RandomSample[#, numofreps] & /@ GatherBy[Position[list, numtorep], First],
            2
            ];
            data
            ]

            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[d, 1, 3]



            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



            0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 1,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0







            share|improve this answer









            $endgroup$

















              Your Answer








              StackExchange.ready(function()
              var channelOptions =
              tags: "".split(" "),
              id: "387"
              ;
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function()
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled)
              StackExchange.using("snippets", function()
              createEditor();
              );

              else
              createEditor();

              );

              function createEditor()
              StackExchange.prepareEditor(
              heartbeatType: 'answer',
              autoActivateHeartbeat: false,
              convertImagesToLinks: false,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: null,
              bindNavPrevention: true,
              postfix: "",
              imageUploader:
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              ,
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              );



              );













              draft saved

              draft discarded


















              StackExchange.ready(
              function ()
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f202332%2fmanipulate-a-list-replace-random-position-of-a-specific-integer-n-times-by-0%23new-answer', 'question_page');

              );

              Post as a guest















              Required, but never shown

























              4 Answers
              4






              active

              oldest

              votes








              4 Answers
              4






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              3












              $begingroup$

              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.






              share|improve this answer









              $endgroup$














              • $begingroup$
                That's great.#lirtosiast. One question more, please. How I can use If function. I want to pick randomly 3 elements from each sublist in matrix d and replace by if 0->1, or 1->0
                $endgroup$
                – Ben Aawf
                Jul 18 at 21:39











              • $begingroup$
                @BenAawf MapAt (used in klgr's answer) is more general than ReplacePart; you can do for instance f = MapAt[1-#&, #, RandomSample[Position[#, 0 | 1], 3]]&, which does your replacement and leaves all elements not 0 or 1 unchanged.
                $endgroup$
                – lirtosiast
                Jul 18 at 21:43















              3












              $begingroup$

              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.






              share|improve this answer









              $endgroup$














              • $begingroup$
                That's great.#lirtosiast. One question more, please. How I can use If function. I want to pick randomly 3 elements from each sublist in matrix d and replace by if 0->1, or 1->0
                $endgroup$
                – Ben Aawf
                Jul 18 at 21:39











              • $begingroup$
                @BenAawf MapAt (used in klgr's answer) is more general than ReplacePart; you can do for instance f = MapAt[1-#&, #, RandomSample[Position[#, 0 | 1], 3]]&, which does your replacement and leaves all elements not 0 or 1 unchanged.
                $endgroup$
                – lirtosiast
                Jul 18 at 21:43













              3












              3








              3





              $begingroup$

              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.






              share|improve this answer









              $endgroup$



              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.







              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Jul 18 at 21:22









              lirtosiastlirtosiast

              4672 silver badges12 bronze badges




              4672 silver badges12 bronze badges














              • $begingroup$
                That's great.#lirtosiast. One question more, please. How I can use If function. I want to pick randomly 3 elements from each sublist in matrix d and replace by if 0->1, or 1->0
                $endgroup$
                – Ben Aawf
                Jul 18 at 21:39











              • $begingroup$
                @BenAawf MapAt (used in klgr's answer) is more general than ReplacePart; you can do for instance f = MapAt[1-#&, #, RandomSample[Position[#, 0 | 1], 3]]&, which does your replacement and leaves all elements not 0 or 1 unchanged.
                $endgroup$
                – lirtosiast
                Jul 18 at 21:43
















              • $begingroup$
                That's great.#lirtosiast. One question more, please. How I can use If function. I want to pick randomly 3 elements from each sublist in matrix d and replace by if 0->1, or 1->0
                $endgroup$
                – Ben Aawf
                Jul 18 at 21:39











              • $begingroup$
                @BenAawf MapAt (used in klgr's answer) is more general than ReplacePart; you can do for instance f = MapAt[1-#&, #, RandomSample[Position[#, 0 | 1], 3]]&, which does your replacement and leaves all elements not 0 or 1 unchanged.
                $endgroup$
                – lirtosiast
                Jul 18 at 21:43















              $begingroup$
              That's great.#lirtosiast. One question more, please. How I can use If function. I want to pick randomly 3 elements from each sublist in matrix d and replace by if 0->1, or 1->0
              $endgroup$
              – Ben Aawf
              Jul 18 at 21:39





              $begingroup$
              That's great.#lirtosiast. One question more, please. How I can use If function. I want to pick randomly 3 elements from each sublist in matrix d and replace by if 0->1, or 1->0
              $endgroup$
              – Ben Aawf
              Jul 18 at 21:39













              $begingroup$
              @BenAawf MapAt (used in klgr's answer) is more general than ReplacePart; you can do for instance f = MapAt[1-#&, #, RandomSample[Position[#, 0 | 1], 3]]&, which does your replacement and leaves all elements not 0 or 1 unchanged.
              $endgroup$
              – lirtosiast
              Jul 18 at 21:43




              $begingroup$
              @BenAawf MapAt (used in klgr's answer) is more general than ReplacePart; you can do for instance f = MapAt[1-#&, #, RandomSample[Position[#, 0 | 1], 3]]&, which does your replacement and leaves all elements not 0 or 1 unchanged.
              $endgroup$
              – lirtosiast
              Jul 18 at 21:43













              5












              $begingroup$

              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







              share|improve this answer











              $endgroup$














              • $begingroup$
                wooow that's so helpful thank you so much @kglr
                $endgroup$
                – Ben Aawf
                Jul 18 at 22:06















              5












              $begingroup$

              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







              share|improve this answer











              $endgroup$














              • $begingroup$
                wooow that's so helpful thank you so much @kglr
                $endgroup$
                – Ben Aawf
                Jul 18 at 22:06













              5












              5








              5





              $begingroup$

              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







              share|improve this answer











              $endgroup$



              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








              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Jul 18 at 22:49

























              answered Jul 18 at 21:23









              kglrkglr

              208k10 gold badges238 silver badges472 bronze badges




              208k10 gold badges238 silver badges472 bronze badges














              • $begingroup$
                wooow that's so helpful thank you so much @kglr
                $endgroup$
                – Ben Aawf
                Jul 18 at 22:06
















              • $begingroup$
                wooow that's so helpful thank you so much @kglr
                $endgroup$
                – Ben Aawf
                Jul 18 at 22:06















              $begingroup$
              wooow that's so helpful thank you so much @kglr
              $endgroup$
              – Ben Aawf
              Jul 18 at 22:06




              $begingroup$
              wooow that's so helpful thank you so much @kglr
              $endgroup$
              – Ben Aawf
              Jul 18 at 22:06











              3












              $begingroup$

              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 *)





              share|improve this answer











              $endgroup$



















                3












                $begingroup$

                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 *)





                share|improve this answer











                $endgroup$

















                  3












                  3








                  3





                  $begingroup$

                  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 *)





                  share|improve this answer











                  $endgroup$



                  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 *)






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Jul 18 at 21:22

























                  answered Jul 18 at 21:16









                  RomanRoman

                  14.9k1 gold badge20 silver badges51 bronze badges




                  14.9k1 gold badge20 silver badges51 bronze badges
























                      1












                      $begingroup$

                      I guess I'll throw my hat in the ring.



                      f[list_, numtorep_, numofreps_] := Block[
                      data = list,
                      Apply[
                      (data[[##]] = 0) &,
                      RandomSample[#, numofreps] & /@ GatherBy[Position[list, numtorep], First],
                      2
                      ];
                      data
                      ]

                      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[d, 1, 3]



                      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



                      0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 1,
                      0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0







                      share|improve this answer









                      $endgroup$



















                        1












                        $begingroup$

                        I guess I'll throw my hat in the ring.



                        f[list_, numtorep_, numofreps_] := Block[
                        data = list,
                        Apply[
                        (data[[##]] = 0) &,
                        RandomSample[#, numofreps] & /@ GatherBy[Position[list, numtorep], First],
                        2
                        ];
                        data
                        ]

                        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[d, 1, 3]



                        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



                        0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 1,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0







                        share|improve this answer









                        $endgroup$

















                          1












                          1








                          1





                          $begingroup$

                          I guess I'll throw my hat in the ring.



                          f[list_, numtorep_, numofreps_] := Block[
                          data = list,
                          Apply[
                          (data[[##]] = 0) &,
                          RandomSample[#, numofreps] & /@ GatherBy[Position[list, numtorep], First],
                          2
                          ];
                          data
                          ]

                          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[d, 1, 3]



                          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



                          0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 1,
                          0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0







                          share|improve this answer









                          $endgroup$



                          I guess I'll throw my hat in the ring.



                          f[list_, numtorep_, numofreps_] := Block[
                          data = list,
                          Apply[
                          (data[[##]] = 0) &,
                          RandomSample[#, numofreps] & /@ GatherBy[Position[list, numtorep], First],
                          2
                          ];
                          data
                          ]

                          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[d, 1, 3]



                          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



                          0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 1,
                          0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0








                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Jul 18 at 22:38









                          That Gravity GuyThat Gravity Guy

                          2,3211 gold badge6 silver badges15 bronze badges




                          2,3211 gold badge6 silver badges15 bronze badges






























                              draft saved

                              draft discarded
















































                              Thanks for contributing an answer to Mathematica Stack Exchange!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid


                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.

                              Use MathJax to format equations. MathJax reference.


                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function ()
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f202332%2fmanipulate-a-list-replace-random-position-of-a-specific-integer-n-times-by-0%23new-answer', 'question_page');

                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              Get product attribute by attribute group code in magento 2get product attribute by product attribute group in magento 2Magento 2 Log Bundle Product Data in List Page?How to get all product attribute of a attribute group of Default attribute set?Magento 2.1 Create a filter in the product grid by new attributeMagento 2 : Get Product Attribute values By GroupMagento 2 How to get all existing values for one attributeMagento 2 get custom attribute of a single product inside a pluginMagento 2.3 How to get all the Multi Source Inventory (MSI) locations collection in custom module?Magento2: how to develop rest API to get new productsGet product attribute by attribute group code ( [attribute_group_code] ) in magento 2

                              Category:9 (number) SubcategoriesMedia in category "9 (number)"Navigation menuUpload mediaGND ID: 4485639-8Library of Congress authority ID: sh85091979ReasonatorScholiaStatistics

                              Magento 2.3: How do i solve this, Not registered handle, on custom form?How can i rewrite TierPrice Block in Magento2magento 2 captcha not rendering if I override layout xmlmain.CRITICAL: Plugin class doesn't existMagento 2 : Problem while adding custom button order view page?Magento 2.2.5: Overriding Admin Controller sales/orderMagento 2.2.5: Add, Update and Delete existing products Custom OptionsMagento 2.3 : File Upload issue in UI Component FormMagento2 Not registered handleHow to configured Form Builder Js in my custom magento 2.3.0 module?Magento 2.3. How to create image upload field in an admin form