Generator problem in code playground | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Generator problem in code playground

I have a code (random int generator) that works fine in IDLE but behaves strangely in code playground. Here on Sololearn it seems to yield the first number over and over. What is wrong with it? https://code.sololearn.com/cE6saubSp1AR/#py Code playground output: 10 random numbers in range(0, 100): 4 4 4 4 4 4 4 4 4 4 IDLE output: 10 random numbers in range(0, 100): 15 35 15 47 27 19 39 3 95 47

29th Nov 2016, 4:57 PM
donkeyhot
donkeyhot - avatar
11 Answers
+ 3
Checking infinite. Edit: def natural(stopat): i = 1 while i<=stopat: yield i i += 1 def infnat(): i=1 while True: yield i i += 1 if __name__ == '__main__': for i in natural(10): print(i) infgen=infnat() # one generator object for i in range(10): print(next(infgen)) # reuse object Outputs: 1..10 (as limited sequence) 1..10 (as infinite sequence) ==== Your code - move rand(a,b) out as r: def test_rand(numtests, a, b): print... r=rand(a,b) for i in range(numtests): print(next(r))
29th Nov 2016, 5:33 PM
Kirk Schafer
Kirk Schafer - avatar
+ 2
edit... http://stackoverflow.com/a/34344919/3981745 Recommends using a UUID. I'm poking at your code, which is returning non-unique times.
28th Nov 2016, 10:59 PM
Kirk Schafer
Kirk Schafer - avatar
+ 2
update 1: ...but that's what stalled me. The function is supposed to resume execution after the last yield(), but when I insert print(seed) after its initialization (print() at line 11), I see the same hash (and actually the same timestamp) 10 times. This made me think I didn't understand the generator. I'll look again.
29th Nov 2016, 4:53 PM
Kirk Schafer
Kirk Schafer - avatar
+ 2
Update 2: I bumped your request from 10 to 1000. About halfway through the output changed, then got stuck on that number.
29th Nov 2016, 4:59 PM
Kirk Schafer
Kirk Schafer - avatar
+ 2
Not the hash...I'm saying it should change which implies you're potentially reinitializing your generator. Using your 'natural' example: def natural(stopat): i = 1 while i<=stopat: yield i i += 1 if __name__ == '__main__': for i in natural(10): print(i)
29th Nov 2016, 5:27 PM
Kirk Schafer
Kirk Schafer - avatar
+ 2
It appears that to make a generator object, you have to assign the generating function to a variable. Otherwise codeplaygroung considers it as just a function and calls it from the very beginning each time: def natural(): i = 1 while True: yield i i += 1 if __name__ == '__main__': nat = natural() print(type(natural)) print(type(nat)) Output: <class 'function'> <class 'generator'>
29th Nov 2016, 6:06 PM
donkeyhot
donkeyhot - avatar
+ 1
Thanks, @Kirk. But the problem has nothing to do with hash(time()). I use it only once in my program as a seed value. Take a look here: https://code.sololearn.com/cx6A4eVw6pNw/#py def natural(): i = 1 while True: yield i i += 1 if __name__ == '__main__': for i in range(10): print(next(natural())) There is no randomness in the code. But it still yields only the first element.
29th Nov 2016, 4:56 PM
donkeyhot
donkeyhot - avatar
+ 1
Update 3: I tried .__next__() and .next() - both problems. But generators maintain state...so I gave the generator limit control by passing numtests and then 'while True' to 'while c < alltests', and changing the request loop from range() to 'in'. This resulted in varying hashes. c=0 while c<alltests: ... c+=1 yield ... ... and below: for i in rand(a,b,numtests): print(i)
29th Nov 2016, 5:14 PM
Kirk Schafer
Kirk Schafer - avatar
+ 1
Just wow @Kirk. Good job. You made it work. Cheers!
29th Nov 2016, 5:45 PM
donkeyhot
donkeyhot - avatar
0
yeah, that's weird... Just contact SoloLearn to report the bug, it's quite peculiar...
28th Nov 2016, 9:06 PM
Amaras A
Amaras A - avatar
0
Yes, limited iterators work fine here. But what if I want an infinite one? And how can i reinitialize a generator unawares?
29th Nov 2016, 5:36 PM
donkeyhot
donkeyhot - avatar