Posts: 404
Threads: 94
Joined: Dec 2017
How would you randomly generate an even number x, 0 <= x < 100? Fill out the definition for the function genEven(). Please generate a uniform distribution over the even numbers between 0 and 100 (not including 100).
my try:
import random
def genEven():
'''
Returns a random even number x, where 0 <= x < 100
'''
# Your code here
rn = random.choice(range(0, 100, 2))
print(rn) Error: 70
TypeError: unorderable types: NoneType() < int
What am I doing wrong?
Posts: 4,220
Threads: 97
Joined: Sep 2016
I take it you are submitting this to a program that checks if your code works correctly. In that case, I bet you need to return rn , not print it. Without an explicit return statement, your function returns None.
Posts: 404
Threads: 94
Joined: Dec 2017
Apr-17-2019, 02:35 AM
(This post was last modified: Apr-17-2019, 02:39 AM by Truman.)
Yes, you're correct.
next task is a bit more difficult:
Write a uniformly distributed stochastic program, stochasticNumber, that returns an even number between 9 and 21.
import random
def stochasticNumber():
'''
Stochastically generates and returns a uniformly distributed even number between 9 and 21
'''
# Your code here
return [x for _ in range(9,22) for x in [random.randint(9,22)] if x % 2 I googled similar solution but it is not accepted. Error is the same as in the first case.
Posts: 4,220
Threads: 97
Joined: Sep 2016
I don't understand why you aren't just using the same solution as for the first part. Why the double loop? You also appear to be returning a list, not a number. However, there is a missing ']', is there more missing code there?
Posts: 404
Threads: 94
Joined: Dec 2017
I just didn't copy it correctly. In this program, I submitted with ']'. Will follow your suggestion.
Posts: 4,646
Threads: 1,493
Joined: Sep 2016
even_random_number = random_number*2
even_random_number = odd_random_number+1
even_random_number = odd_random_number+another_odd_random_number
Tradition is peer pressure from dead people
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Posts: 2,125
Threads: 11
Joined: May 2017
Apr-17-2019, 01:04 PM
(This post was last modified: Apr-17-2019, 01:04 PM by DeaD_EyE.)
import random
NUMBERS = list(range(2, 100, 2)) # 2, 4, ... 98
def rnd_even():
return random.choice(NUMBERS)
even_random_vals = [rnd_even() for _ in range(10)] With so less numbers, you can prepare a list with even numbers an let random.choice choose values.
PS: If you need guaranteed entropy (encryption), this is the wrong way.
Posts: 11
Threads: 1
Joined: Mar 2017
hi dear,
here is my code for your question ,
import random
random.randrange(0,100,2)
Posts: 404
Threads: 94
Joined: Dec 2017
Apr-17-2019, 11:11 PM
(This post was last modified: Apr-17-2019, 11:11 PM by Truman.)
Hi Everybody,
Forgot to mention that in this second assignment even number between 9 and 21 should be returned.
The first problem is already solved. :)
and the second one:
import random
def stochasticNumber():
'''
Stochastically generates and returns a uniformly distributed even number between 9 and 21
'''
# Your code here
for x in range(14):
rn = random.choice(range(9, 22))
if rn % 2 == 0:
return rn
Posts: 4,646
Threads: 1,493
Joined: Sep 2016
Apr-17-2019, 11:51 PM
(This post was last modified: Apr-17-2019, 11:51 PM by Skaperen.)
i like random.randrange(10,22,2) . it's 2 characters shorter and more concise (eliminates 2 lines ... it can be returned directly). i don't know if that makes it more pythonic, but i would hope so.
Tradition is peer pressure from dead people
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
|