0

I need to generate 100 random values between 0 and 500 and store the even values in a list and the odd in an other one. I am stuck in the beginning.

import random

def randomval():

   size=random.randint(0, 500)
   return 
   if val%2==0:
       listeven.append()
   else:
       listodd.append()
4
  • And what have you tried to test if a value is odd or even? Or are you stuck because the randomval() function doesn't return what you expected to get? Commented Oct 29, 2013 at 11:31
  • you have to return size. then call randomval() function 100 times. then you are done. Commented Oct 29, 2013 at 11:34
  • (Sorry I'm lazy and just posted a solution; probably just hinting on what is to be done would have been better at this looking-like-homework question.) Commented Oct 29, 2013 at 11:34
  • 1
    That's already a lot better, the return is now in the way and you seem to have forgotten to include a loop, but it's more of an attempt at coding it yourself now. Commented Oct 29, 2013 at 11:37

3 Answers 3

4
both = [ random.randint(0, 500) for i in range(100) ]
odd = [ x for x in both if x % 2 == 1 ]
even = [ x for x in both if x % 2 == 0 ]
Sign up to request clarification or add additional context in comments.

3 Comments

please fix if statements
x & 1 is usually slightly faster, despite being less obvious.
Yeah I had mixed & 1 and % 2 unsuccessfully ;-)
2

Solution depends on whether random number can be repeated or no.

random_values = [ random.randint(0, 500) for i in range(100) ]
odd = [ x for x in random_values if x % 2 ]
even = [ x for x in random_values if not x % 2 ]

And If You would like to get unique random number I would propose You to use shuffle

random_values = random.sample(range(0, 500), 100)
odd = [ x for x in random_values if x % 2 ]
even = [ x for x in random_values if not x % 2 ]

You can reduce memory usage using generators for example

random_values = (random.randint(0, 500) for i in xrange(100))
odd = []
even = []
for val in random_values:
    if val % 2:
        odd.append(val)
    else:
        even.append(val)

Comments

2

Do it all in One Pass - much more efficient.

from random import randint

even, odd = [], []
for _ in range(100):
    num = randint(0, 500)
    if num % 2:
        odd.append(num)
    else:
        even.append(num)

As a function:

def get_random_even_and_odd(amount=100, _min=0, _max=500):
    even, odd = [], []
    for _ in range(amount):
        num = randint(_min, _max)
        if num % 2:
            odd.append(num)
        else:
            even.append(num)
    return even, odd

4 Comments

Ick, don't use conditional expressions when you should really use an if statement. Just use if num % 2: odd.append(num), else: even.append(num). If you have to use a one-liner, odd if num % 2 else even).append(num) is possibly uglier but at least you use the conditional expression as an actual expression for its own sake.
Generally right, @MartijnPieters, but I see a trade-off if using the if statement would mean to double the code massively. In this case it is just the .append(num) which gets doubled so it doesn't matter that much, but if that part is larger, then I'd prefer the conditional expression you proposed.
@Alfe: Don't confuse one-liners with readability. Using a conditional expression creates certain expectations, namely that the outcome of the whole expression is to be used for something. Instead, Inbar was just using it for the side effects (one of two mutables changed).
Of course, the Inbar way (so to speak) was an abuse. I only compared statements (four lines, doubled code) to conditional (one line as you proposed it). And, btw, conditionals can be spread over several lines to increase readability. Did a lot of functional programming earlier, have gotten used to it. But of course, aspects like code-doubling or more objective than readability.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.