Evan Nichols
Evan Nichols

Reputation: 13

Random Number Generator Only Printing One Number

This is the code I have so far:

 from quick_pick import quick_pick
    def main():
     LIMIT = 67
     number = 9
     list_1 = []*number
     quick_pick(number, LIMIT, list_1)
     print (list_1)
     main()

import random
def quick_pick(n,limit,lottery):
    main_count = 0
    while main_count <n:
        lotto_numbers = random.randint(1, limit)
        if lotto_numbers not in lottery:
            lottery.append(lotto_numbers)
            main_count += 1
            return (lottery * n)

but when I run it I get this: [21]

Im not sure how to get all 9 numbers to show up in the list so I can print it. If someone could help it would be appreciated as this is for part of my assignment and I need it to do the rest of it.

Upvotes: 1

Views: 132

Answers (3)

Idos
Idos

Reputation: 15320

Fix you indentation, lose the * n, and viola(!), your code works:

def quick_pick(n,limit,lottery):
    main_count = 0
    while main_count <n:
        lotto_numbers = random.randint(1, limit)
        if lotto_numbers not in lottery:
            lottery.append(lotto_numbers)
            main_count += 1
    return lottery

>>> quick_pick(number,LIMIT,list_1)
[44, 43, 62, 13, 11, 25, 36, 29, 15]

The problem was, as you can see, the fact that you return after finding the first number to add to your lottery. You need to wait until it is filled up. Also multiplying by n (9) doesn't make much sense.

Upvotes: 0

Luca
Luca

Reputation: 11016

Another way to get random numbers following some particular distribution is to use the probability distributions directly..

Example:

import numpy as np

nums = np.random.uniform(-1, 1, (100, 2))

This will generate a hundred 2-dimensional points in the range (-1, 1) from the underlying uniform distribution (all the numbers in the range have the same probability of being picked).

Upvotes: 0

niemmi
niemmi

Reputation: 17273

You can use random.sample to pick the numbers:

limit = 67
n = 9
print(random.sample(range(1, limit + 1), n)) # [49, 32, 66, 57, 25, 9, 22, 4, 48]

Upvotes: 2

Related Questions