Markov chain generator
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
add a comment |
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
2
Why are you looping overrange(1)
?
– user2357112
Nov 22 '18 at 1:03
Replacefor ...
withwhile True:
maybe? And increase the initial value in every step
– user8408080
Nov 22 '18 at 1:13
add a comment |
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
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
python generator markov-chains markov
asked Nov 22 '18 at 1:01
roadrunnerroadrunner
206
206
2
Why are you looping overrange(1)
?
– user2357112
Nov 22 '18 at 1:03
Replacefor ...
withwhile True:
maybe? And increase the initial value in every step
– user8408080
Nov 22 '18 at 1:13
add a comment |
2
Why are you looping overrange(1)
?
– user2357112
Nov 22 '18 at 1:03
Replacefor ...
withwhile 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
add a comment |
2 Answers
2
active
oldest
votes
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
add a comment |
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])
.
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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
add a comment |
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
add a comment |
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
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
answered Nov 22 '18 at 1:25
chrischris
898814
898814
add a comment |
add a comment |
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])
.
add a comment |
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])
.
add a comment |
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])
.
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])
.
answered Nov 22 '18 at 1:22
user8408080user8408080
1,4801310
1,4801310
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
2
Why are you looping over
range(1)
?– user2357112
Nov 22 '18 at 1:03
Replace
for ...
withwhile True:
maybe? And increase the initial value in every step– user8408080
Nov 22 '18 at 1:13