user3023323
user3023323

Reputation: 39

Python random numbers loop

Hey I have a class exercise that I'm stuck with and would appreciate any help. The program is supposed to random select a number between 1-5 and loop until all numbers are picked. Then it's supposed to repeat about 100 times to give an average of how many picks it takes to get all five numbers. I know how to use random.int, just not sure how to write a loop where it breaks when all 5 numbers are picked. Any sort of help would be great. Thank you

Upvotes: 0

Views: 39440

Answers (4)

Developer
Developer

Reputation: 8400

Here is yet another way we may use:

import random

total = {1:0,2:0,3:0,4:0,5:0}

for k in xrange(100):                          #outter loop
    trial = {}
    n = 0                                      #counter
    while n<5:
        sample = random.randint(1,5)           #sample
        try:
            trial[sample] += 1
        except:
            n += 1
            trial[sample] = 1
    for i in xrange(1,6):                      #accumulate the results
        total[i] += trial[i]

which results in:

>>> total
{1: 221, 2: 255, 3: 246, 4: 213, 5: 243}

Upvotes: 0

user2555451
user2555451

Reputation:

This will do it:

from random import randint
a = []
for _ in range(100):
    b = 0
    c = set()
    while len(c) < 5:
        c.add(randint(1, 5))
        b += 1
    a.append(b)
d = round(sum(a)/len(a))
print("{}\nAverage loops: {}".format(c, d))

Documented version:

# Import the `randint` function from `random`.
from random import randint
# This will hold the results that will later be used to calculate the average.
a = []
# This loops 100 times (to get data for a calculation of the average).
for _ in range(100):
    # This will be the number of times the while-loop loops.
    b = 0
    # This is a set to hold the choices of `random.randint`.
    # When it reaches 5 items, all numbers from 1-5 have been found.
    c = set()
    # Loop while the length of the set is less than 5.
    while len(c) < 5:
        # Add a new random number to the set,
        # If the number is already there, nothing happens.
        c.add(randint(1, 5))
        # Record a completed iteration of the while-loop.
        b += 1
    # Add the number of while-loop loops to `a`.
    a.append(b)
# The average is calculated by dividing the sum of `a` by the length of `a`.
# It is then rounded to the nearest integer.
d = round(sum(a)/len(a))
# Print the results.
print("{}\nAverage loops: {}".format(c, d))

Sample output:

{1, 2, 3, 4, 5}
Average loops: 12

{1, 2, 3, 4, 5}
Average loops: 10

{1, 2, 3, 4, 5}
Average loops: 9

Upvotes: 2

Reacto
Reacto

Reputation: 11

I would just do a while-loop that generates a random number between 1-5 until it gets the first number, t, logging the number of tries it took., logging the number of tries it took.hen do that with the next number, and when you're done find the average of how many tries it took.

Example:

total = 0
for i in range(100):
    for n in range(1,6):
        generatedNum = 0
        while generatedNum != n:
            #generate random number
            total += 1
print (total/100)

Upvotes: 1

Paul
Paul

Reputation: 673

I would suggest you to use a Set. Each time a value between 1-5 is reached, put it in the your set, then when the size of the set is 5, break the loop.

Upvotes: 3

Related Questions