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

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

                              Circuit construction for execution of conditional statements using least significant bitHow are two different registers being used as “control”?How exactly is the stated composite state of the two registers being produced using the $R_zz$ controlled rotations?Efficiently performing controlled rotations in HHLWould this quantum algorithm implementation work?How to prepare a superposed states of odd integers from $1$ to $sqrtN$?Why is this implementation of the order finding algorithm not working?Circuit construction for Hamiltonian simulationHow can I invert the least significant bit of a certain term of a superposed state?Implementing an oracleImplementing a controlled sum operation

                              Magento 2 “No Payment Methods” in Admin New OrderHow to integrate Paypal Express Checkout with the Magento APIMagento 1.5 - Sales > Order > edit order and shipping methods disappearAuto Invoice Check/Money Order Payment methodAdd more simple payment methods?Shipping methods not showingWhat should I do to change payment methods if changing the configuration has no effects?1.9 - No Payment Methods showing upMy Payment Methods not Showing for downloadable/virtual product when checkout?Magento2 API to access internal payment methodHow to call an existing payment methods in the registration form?