Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6204113
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T05:02:02+00:00 2026-05-24T05:02:02+00:00

For some strange reason, let’s say that I want use List Comprehensions — and

  • 0

For some strange reason, let’s say that I want use List Comprehensions — and List Comprehensions only — to generate 10 random numbers larger than 0.5. For the “10 random numbers” part of the problem we would use:

samples = [ random.random() for x in range(10) ]

Now, for the “larger than 0.5”, how one would implement using LC?

samples = [ random.random() for x in range(10) if ??? ]
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T05:02:04+00:00Added an answer on May 24, 2026 at 5:02 am

    What are you describing is called rejection sampling, and is a bad idea. For example if you wanted only numbers >0.999, it would take 1000 times longer to generate them.


    Best way

    The correct way to do this is to use another sampling technique, such as using the inverse of the CDF (cumulative density function), and doing [inverseCDF(random()) for _ in range(10)]. In your case, this would be, like GaretJax suggests, [0.5+random()/2 for _ in range(10)]


    Intent

    I think your intent was being able to further filter based on the entire expression of a list comprehension. In that case, you would put your iterable (here with parentheses rathern than […] for laziness, though it doesn’t matter) inside another comprehension:

    [r for r in (random.random() for x in range(10)) if r>0.5]
    

    For a better solution along these lines that can handle rejection, see the “Solution with list comprehensions only” section or eryksun’s answer.


    Solution with generators

    (see below for a list-comprehension only solution)

    You can do this for any linear range. If your rejection function is arbitrarily complex however, a general way to do rejection sampling would be as follows. (Again, a very bad idea unless you know what you’re doing and efficiency doesn’t matter.)

    from random import random
    from itertools import *
    
    def randoms():
        while True:
            yield random()
    
    def rejectionSample(pred, n):
        return islice(filter(pred, randoms()), n)
    

    Example:

    >>> print( list(rejectionSample(lambda x:x>0.5)) )
    [0.6656564857979361, 0.9850389778418555, 0.9607471536139308, 0.9191328900300356, 0.810783093197139]
    

    You could also do something like:

    def rejectionSample(pred, n):
        count = 0
        while count<n:
            r = random()
            if pred(r):
                yield r
                count += 1 
    

    Solution with list comprehensions only

    However since you want to use list comprehensions only, this means the expression-part of your comprehension cannot fail, so you have to somehow embed a while loop in the comprehension. This is impossible to do with a single lambda function alone, but we can pull it off as long as we have some recursive/loopy primitive, for example…

    [next(filter(pred,randoms())) for _ in range(10)]
    

    (If you really wanted a one-liner list comprehension, randoms() can be rewritten as (random() for _ in count()).) Again, this is unnecessary if you easily find the analytic inverse cumulative distribution function for your particular distribution.


    edit: I take that back… it… is possible… with just lambdas…

    DEAR GOD HAVE MERCY WHAT HORRORS HAVE I UNLEASHED UPON THE WORLD NOOOOOO

    [
     (lambda f:f(f,random()))(lambda self,r:r if r>0.5 else self(self,random()))
     for _ in range(10)
    ]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some strange reason Comic Sans disappeared from my list of available fonts. The
I have a pretty basic question that confuses me for some strange reason. I
For some strange reason, a five year old internal asp.net web app that is
So, for some strange reason I end up with a 100GB log file that
well for some strange reason IE gives me and InvalidAuthenticityToken error almost every time
I'm passing two strings between two activities and for some strange reason, the strings
For some strange, bizarre reason, my images in my website just will not display
I'm having some strange drawing artefacts that I'm hoping someone might be able to
For some strange reason, my text-align: center; isn't working. It's weird 'cause it's the
For some strange reason, the \n and \r escape sequences do not seem to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.