7

I'm using random.random() to get a random float (obviously!). But what I really want to do is something like:

there's a 30% chance my app does this:
  pass
else:
  pass

Can you guys help me structure this?

3
  • What are you really trying to do? Between the vagueness of the question and the strange comment thread regarding the nature of randomness on @hd1's answer below, it's quite unclear. Commented Dec 8, 2012 at 2:24
  • @Dkamins, I edited the post to make it a little more clear, I hope. I need it to perform an action 30% of the time, otherwise, do something else. Just looking for a general algorithm. hd1's post seems to be it - I'm just trying to understand it a little bit more before I accept. Commented Dec 8, 2012 at 2:30
  • If you're thinking of "30%" as a float value, accept hd1's answer; if you're thinking of it as "3 out of 10", accept Óscar López's answer. They're both correct, and equivalent, it's just a matter of which is more readable or seems more "right" in your case. Commented Dec 8, 2012 at 2:34

2 Answers 2

12
if random.random() > 0.5: 
    # your app does this 
    pass
else: 
    # your app does that
    pass
Sign up to request clarification or add additional context in comments.

15 Comments

It's probably easier to use random() < x since x would directly correspond to the probability of the condition's being true.
But does this answer the question? The asker wants to make it so that if he flips a coin repeatedly, it gives randomly ordered results, skewed 40% (or whatever) towards heads. Your answer could feasibly give 0% heads.
@irrelephant Yes, but that could just keep returning something greater than 0.5, 100% of the time. It is not guaranteed to give an X% chance, like Tyler wants. Unless..and here my understanding of the random module falls short...the distribution of random.random() is completely uniform, so exactly half the results will be greater than 0.5 and half of them will be less? In that case, this answer is correct, and I'm mistaken.
@Aerovistae: Even a truly random sequence could just keep returning something greater than 0.5, 100% of the time. That's a perfectly reasonable result for something that has a 50% chance of happening. It's not the same thing as happening 50% of the times when run N times (except as N approaches infinity), but the OP asked for the former, not the latter. In other words, it's perfectly reasonable for something with a 50% chance of happening to happen, say, 9 times in 20 runs, or even 0 (it's just not that likely—the odds are 1/2^N).
@TylerSeymour: Yes, he's used .5 to mean a 50% chance; change it to .3 to mean a 30% chance. Most people would write this as random.random() < .3, but of course .3 > random.random() means the exact same thing, as would the answer's random.random() > .7.
|
6

Try this:

if random.randint(1, 10) in (1, 2, 3):
    print '30% chance'
else:
    print '70% chance'

Here randint will generate a number between 1-10, there's a 30% chance that it's between 1-3 and a 70% chance that it's between 4-10

2 Comments

Less flexible than @hd1's method, since this can only do equal probabilities, but convenient. Note that you could also skip the == 1 if you wanted.
This is a better solution when your probabilities are best described as "1 in 2" or "1 in 3". Of course for a rational number, you have a choice of describing it either way, so if random.randrange(10) < 3 is just as reasonable as random.random() < .3; it's just a matter of which one seems clearer to you.

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.