0

for:

import random
keys='abcdefghijklmnopqrstuvwxyz '
random.choice(keys)

How would I count how many times it takes random.choices(keys) to generate "m"?

3
Contributors
5
Replies
6
Views
7 Years
Discussion Span
Last Post by D33wakar
0

do u mean like:

import random
summy=0
keys='abcdefghijklmnopqrstuvwxyz '

for i in range(100):
    y=random.choice(keys)
    if y!='m':
        summy+=1
    elif y == 'm':
        continue
print summy
1

Perhaps not the best way to do it, but maybe something like this?

import random

tries = 0
keys='abcdefghijklmnopqrstuvwxyz '
key = random.choice(keys)

while key != "m":             #While key is not equal to 'm'
    tries += 1                #Add one to tries
    key = random.choice(keys) #Choose another key

if key == "m":                #Key 'm' was chosen
    print "Tried %s times until we got m." % tries
Votes + Comments
geat post
0

Perhaps not the best way to do it, but maybe something like this?

import random

tries = 0
keys='abcdefghijklmnopqrstuvwxyz '
key = random.choice(keys)

while key != "m":             #While key is not equal to 'm'
    tries += 1                #Add one to tries
    key = random.choice(keys) #Choose another key

if key == "m":                #Key 'm' was chosen
    print "Tried %s times until we got m." % tries

Yes! Exactly like that! Thank you so much! yahoo!

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.