Python Forum
Randomly generate an even number - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: Randomly generate an even number (/thread-17581.html)

Pages: 1 2


Randomly generate an even number - Truman - Apr-17-2019

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?


RE: Randomly generate an even number - ichabod801 - Apr-17-2019

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.


RE: Randomly generate an even number - Truman - Apr-17-2019

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.


RE: Randomly generate an even number - ichabod801 - Apr-17-2019

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?


RE: Randomly generate an even number - Truman - Apr-17-2019

I just didn't copy it correctly. In this program, I submitted with ']'. Will follow your suggestion.


RE: Randomly generate an even number - Skaperen - Apr-17-2019

even_random_number = random_number*2
even_random_number = odd_random_number+1
even_random_number = odd_random_number+another_odd_random_number



RE: Randomly generate an even number - DeaD_EyE - Apr-17-2019

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.


RE: Randomly generate an even number - ankit - Apr-17-2019

hi dear,
here is my code for your question ,

import random
random.randrange(0,100,2)


RE: Randomly generate an even number - Truman - Apr-17-2019

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



RE: Randomly generate an even number - Skaperen - Apr-17-2019

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.