FAQ: Modules: Python - Modules Python Random

2 posts were split to a new topic: What does random.randint() do?

3 posts were split to a new topic: When do we need from and import vs just import?

2 posts were split to a new topic: How can I set a random value during my code?

Isn’t this list comprehension from solution redundant?

random_list = [random.randint(1,101) for i in range(101)]

Firt part is already saying that we want a random number in range from 1 to 101. Why do we need the last part saying the same thing again? When I tried to change it to any number, even 1, I still got random numbers from 1 to 101.

1 Like

What do you argue it should be instead?
What is the purpose of it that it’s supposed to implement?
Would you still be satisfying that purpose with your proposed alternative, after removing whatever you are arguing is redundant?

I have a feeling that we put it there only because list comprehension is like [X for Y in Z] so it just has to have some Z. If I remove last part I get syntax error. But when I put ANY >0 number in there it works just fine, doesn’t matter if it’s 1 or 100000.

Note that the range is only used to count the number of iterations. The result is 101 numbers between 1 and 101 inclusive.

What do you mean by “just fine”?
You have to consider what the purpose is.
What does that part of a list comprehension do, what effect would changing it therefore do?
Why is list comprehension used at all?

Anyway I have no clue what it’s supposed to do, so I couldn’t possibly tell whether it’s doing something redundant, but it does do things and changing inputs to it will have effects in the result and whether that matters comes down to the purpose of it all… (and again, you should be considering the intentions before you consider the code) But what the effect of changing it is, well, ask yourself what that part of list comprehension does, you need to know that to reason about list comprehension anyway, you can’t begin to say it’s redundant before you know what it does

0 is still there.

Anyway, after looking at the exercise, what’s redundant is in the idea that is implemented.
…so consider the intention before considering the code
the redundancy is in the intention

If you create a list of random numbers
And then choose a random value from that list.
Then your result is equivalent to generating a single random value, the list is therefore redundant.
What does that have to do with list comprehension? It doesn’t, it’s the whole plan/concept/idea/intention that’s bad, and that’s the thing to reason about, not list comprehension.

D’oh! Was thinking zero and wrote 1.

We are supposed to make a list that provides us with 100 different numbers between 1 and 100.
Using the code provided from the solution:

random_list = [random.randint(1,101) for i in range(101)]

if I print the random_list I do see the number 101 showing up amongst the 100 numbers.

So my question is:
Does the argument in random.randint() not work the same as in range()?
By that, I mean how the first argument is inclusive while the second argument is exclusive.

If I change the second argument in random.randint() from 101 to 100 I only observe numbers between 1 and 100.

1 Like

… is not an argument, but a value to be inserted into the resultant list.

documentation for those functions will tell you how they behave

It should be like this I think

random_list = [random.randint(0,x) for x in range(101)]

x should be a constant, not a variable. The range is only used to count to 100.

random.randint(0, 100)
1 Like

yeah you’re correct because in the ques it is said to generate a random integer between 1 and 100 (inclusive) for each number in range(101) mean it should always generate random numbers between 1 and 100 but in my case where I’ve assumed x to be variable it is generating random numbers between 1 and x. Strange thing is that my solution was still accepted because I guess the question was looking more for answer than solution. Thank you for correcting me.

1 Like

On this page, Codecademy asks us to do the following:

1. In script.py import the random library.

2. Create a variable random_list and set it equal to an empty list

3. Turn the empty list into a list comprehension that uses random.randint() to generate a random integer between 1 and 100 (inclusive) for each number in range(101) .

4. Create a new variable randomer_number and set it equal to random.choice() with random_list as an argument.

5. Print randomer_number out to see what number was picked!

I have the following solution, which uses list comprehension, but I don’t understand why this is needed at all compared to another solution I came up with (commented below my solution). Could someone explain what’s the point of using a list?

# Import random below:
import random

# Create random_list below:
random_list = []

# Create randomer_number below:
for i in range(101):

randomer_number = random.choice(random_list)
# Print randomer_number below:

# Why not use this? It's much shorter. 
# It seems to do the same thing, unless I'm mistaken.
#import random

Is all your code there (above)? We do not see any list comprehension.

Weird, codecademy accepted my solution. I looked up list comprehensions and seem to have misunderstood what it was.

Would it work if I replace this:

for i in range(101):

by this?

random_list = [random.randint(1,101) for i in range(101)]

But my question remains the same: why use all of this code, instead of the bit that I commented at the end? It seems to have the same result.

# Why not use this? It's much shorter. 
# It seems to do the same thing, unless I'm mistaken.
#import random

Yes, since the exercise asks for a comprehension. As for why not use your last example, it is likely so we get to practice with both comprehension and random examples.

In your earlier example you use random_list.choice() and while the outcome may look like the simpler form, when we consider this list, it is not a full sample set. We generate 100 random numbers in the range of 1 to 100, but there is no stipulation that it is just a shuffled list. There can be multiple duplicates and multiple omitted values in the list.


>>> import random
>>> random_list = [random.randint(1,101) for i in range(101)]
>>> len(set(random_list))

So of the 100 values in the list, 39 of them are duplicates which means 39 values were omitted. Where your last example has equal probability of any number 1 to 100, this does not. One rather doubts that is what the author wanted us to see, but it is definitely different.

1 Like

Thank you! Very helpful and kind of you to take the time to provide explanations!

1 Like