Markov chain generator












-1















The generator should take a starting point (an integer). With each pass of the resultant generator object to next, a random step from the last point returned (or the starting point if no point has yet been returned) should be performed. The result of this step should be returned after the step is taken.



import random

def markov(start: int):
for i in range (1):
yield random.randint(i-1, i+1)


What is wrong with my code?










share|improve this question


















  • 2





    Why are you looping over range(1)?

    – user2357112
    Nov 22 '18 at 1:03











  • Replace for ... with while True: maybe? And increase the initial value in every step

    – user8408080
    Nov 22 '18 at 1:13


















-1















The generator should take a starting point (an integer). With each pass of the resultant generator object to next, a random step from the last point returned (or the starting point if no point has yet been returned) should be performed. The result of this step should be returned after the step is taken.



import random

def markov(start: int):
for i in range (1):
yield random.randint(i-1, i+1)


What is wrong with my code?










share|improve this question


















  • 2





    Why are you looping over range(1)?

    – user2357112
    Nov 22 '18 at 1:03











  • Replace for ... with while True: maybe? And increase the initial value in every step

    – user8408080
    Nov 22 '18 at 1:13
















-1












-1








-1








The generator should take a starting point (an integer). With each pass of the resultant generator object to next, a random step from the last point returned (or the starting point if no point has yet been returned) should be performed. The result of this step should be returned after the step is taken.



import random

def markov(start: int):
for i in range (1):
yield random.randint(i-1, i+1)


What is wrong with my code?










share|improve this question














The generator should take a starting point (an integer). With each pass of the resultant generator object to next, a random step from the last point returned (or the starting point if no point has yet been returned) should be performed. The result of this step should be returned after the step is taken.



import random

def markov(start: int):
for i in range (1):
yield random.randint(i-1, i+1)


What is wrong with my code?







python generator markov-chains markov






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 22 '18 at 1:01









roadrunnerroadrunner

206




206








  • 2





    Why are you looping over range(1)?

    – user2357112
    Nov 22 '18 at 1:03











  • Replace for ... with while True: maybe? And increase the initial value in every step

    – user8408080
    Nov 22 '18 at 1:13
















  • 2





    Why are you looping over range(1)?

    – user2357112
    Nov 22 '18 at 1:03











  • Replace for ... with while True: maybe? And increase the initial value in every step

    – user8408080
    Nov 22 '18 at 1:13










2




2





Why are you looping over range(1)?

– user2357112
Nov 22 '18 at 1:03





Why are you looping over range(1)?

– user2357112
Nov 22 '18 at 1:03













Replace for ... with while True: maybe? And increase the initial value in every step

– user8408080
Nov 22 '18 at 1:13







Replace for ... with while True: maybe? And increase the initial value in every step

– user8408080
Nov 22 '18 at 1:13














2 Answers
2






active

oldest

votes


















0














You're on the right track. Assuming a step-size of 1 (in either direction), you should be able to get rid of the i counter altogether:



>>> import random
>>>
>>>
>>> def markov(start: int):
... location = start
... while True:
... yield location
... location += random.randint(-1, 1)
...
>>>
>>> gen = markov(5)
>>> next(gen)
5
>>> next(gen)
6
>>> next(gen)
5
>>> next(gen)
6
>>> next(gen)
7
>>> next(gen)
6
>>> next(gen)
5
>>> next(gen)
4
>>> next(gen)
4
>>> next(gen)
3





share|improve this answer































    0














    I introduced a count for easy testing, but you could just go with an endless loop. This will go 1 back or further than the last step or stay at the last step.



    import random

    def markov(start: int):
    step = start
    count = 0
    while count<20:
    step = random.randint(step-1, step+1)
    yield step
    count += 1


    You can test if this is what you want by list(markov(1)) for example. If you always want it to move, I would replace step = random.randint(step-1, step+1) with step = random.choice([step-1, step+1]).






    share|improve this answer























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      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: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      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%2fstackoverflow.com%2fquestions%2f53422526%2fmarkov-chain-generator%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0














      You're on the right track. Assuming a step-size of 1 (in either direction), you should be able to get rid of the i counter altogether:



      >>> import random
      >>>
      >>>
      >>> def markov(start: int):
      ... location = start
      ... while True:
      ... yield location
      ... location += random.randint(-1, 1)
      ...
      >>>
      >>> gen = markov(5)
      >>> next(gen)
      5
      >>> next(gen)
      6
      >>> next(gen)
      5
      >>> next(gen)
      6
      >>> next(gen)
      7
      >>> next(gen)
      6
      >>> next(gen)
      5
      >>> next(gen)
      4
      >>> next(gen)
      4
      >>> next(gen)
      3





      share|improve this answer




























        0














        You're on the right track. Assuming a step-size of 1 (in either direction), you should be able to get rid of the i counter altogether:



        >>> import random
        >>>
        >>>
        >>> def markov(start: int):
        ... location = start
        ... while True:
        ... yield location
        ... location += random.randint(-1, 1)
        ...
        >>>
        >>> gen = markov(5)
        >>> next(gen)
        5
        >>> next(gen)
        6
        >>> next(gen)
        5
        >>> next(gen)
        6
        >>> next(gen)
        7
        >>> next(gen)
        6
        >>> next(gen)
        5
        >>> next(gen)
        4
        >>> next(gen)
        4
        >>> next(gen)
        3





        share|improve this answer


























          0












          0








          0







          You're on the right track. Assuming a step-size of 1 (in either direction), you should be able to get rid of the i counter altogether:



          >>> import random
          >>>
          >>>
          >>> def markov(start: int):
          ... location = start
          ... while True:
          ... yield location
          ... location += random.randint(-1, 1)
          ...
          >>>
          >>> gen = markov(5)
          >>> next(gen)
          5
          >>> next(gen)
          6
          >>> next(gen)
          5
          >>> next(gen)
          6
          >>> next(gen)
          7
          >>> next(gen)
          6
          >>> next(gen)
          5
          >>> next(gen)
          4
          >>> next(gen)
          4
          >>> next(gen)
          3





          share|improve this answer













          You're on the right track. Assuming a step-size of 1 (in either direction), you should be able to get rid of the i counter altogether:



          >>> import random
          >>>
          >>>
          >>> def markov(start: int):
          ... location = start
          ... while True:
          ... yield location
          ... location += random.randint(-1, 1)
          ...
          >>>
          >>> gen = markov(5)
          >>> next(gen)
          5
          >>> next(gen)
          6
          >>> next(gen)
          5
          >>> next(gen)
          6
          >>> next(gen)
          7
          >>> next(gen)
          6
          >>> next(gen)
          5
          >>> next(gen)
          4
          >>> next(gen)
          4
          >>> next(gen)
          3






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 22 '18 at 1:25









          chrischris

          898814




          898814

























              0














              I introduced a count for easy testing, but you could just go with an endless loop. This will go 1 back or further than the last step or stay at the last step.



              import random

              def markov(start: int):
              step = start
              count = 0
              while count<20:
              step = random.randint(step-1, step+1)
              yield step
              count += 1


              You can test if this is what you want by list(markov(1)) for example. If you always want it to move, I would replace step = random.randint(step-1, step+1) with step = random.choice([step-1, step+1]).






              share|improve this answer




























                0














                I introduced a count for easy testing, but you could just go with an endless loop. This will go 1 back or further than the last step or stay at the last step.



                import random

                def markov(start: int):
                step = start
                count = 0
                while count<20:
                step = random.randint(step-1, step+1)
                yield step
                count += 1


                You can test if this is what you want by list(markov(1)) for example. If you always want it to move, I would replace step = random.randint(step-1, step+1) with step = random.choice([step-1, step+1]).






                share|improve this answer


























                  0












                  0








                  0







                  I introduced a count for easy testing, but you could just go with an endless loop. This will go 1 back or further than the last step or stay at the last step.



                  import random

                  def markov(start: int):
                  step = start
                  count = 0
                  while count<20:
                  step = random.randint(step-1, step+1)
                  yield step
                  count += 1


                  You can test if this is what you want by list(markov(1)) for example. If you always want it to move, I would replace step = random.randint(step-1, step+1) with step = random.choice([step-1, step+1]).






                  share|improve this answer













                  I introduced a count for easy testing, but you could just go with an endless loop. This will go 1 back or further than the last step or stay at the last step.



                  import random

                  def markov(start: int):
                  step = start
                  count = 0
                  while count<20:
                  step = random.randint(step-1, step+1)
                  yield step
                  count += 1


                  You can test if this is what you want by list(markov(1)) for example. If you always want it to move, I would replace step = random.randint(step-1, step+1) with step = random.choice([step-1, step+1]).







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 22 '18 at 1:22









                  user8408080user8408080

                  1,4801310




                  1,4801310






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • 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.


                      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%2fstackoverflow.com%2fquestions%2f53422526%2fmarkov-chain-generator%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

                      If I really need a card on my start hand, how many mulligans make sense? [duplicate]

                      Alcedinidae

                      Can an atomic nucleus contain both particles and antiparticles? [duplicate]