Python Forum
Randomly generate an even number
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Randomly generate an even number
#1
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?
Reply
#2
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
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.
Reply
#4
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?
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
I just didn't copy it correctly. In this program, I submitted with ']'. Will follow your suggestion.
Reply
#6
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.
Reply
#7
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.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#8
hi dear,
here is my code for your question ,

import random
random.randrange(0,100,2)
Reply
#9
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
Reply
#10
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Using randomly generated lists souprqtpie 3 2,518 Apr-19-2019, 07:46 PM
Last Post: souprqtpie

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020