0

I am creating a 'Euromillions Lottery generator' just for fun and I keep getting the same numbers printing out. How can I make it so that I get random numbers and never get the same number popping up:

from random import randint

numbers = randint(1,50)

stars = randint(1,11)


print "Your lucky numbers are: ", numbers, numbers, numbers, numbers, numbers

print "Your lucky stars are: " , stars, stars

The output is just:

>>> Your lucky numbers are:  41 41 41 41 41
>>> Your lucky stars are:  8 8
>>> Good bye!

How can I fix this?

Regards

1
  • numbers = randint(1, 50) does not mean "numbers shall be a magical thing that evaluates randint(1, 50) each time I look at it and tells me the result". It means "evaluate randint(1, 50) right now; numbers shall be a name for the result". Commented Jun 25, 2013 at 11:09

6 Answers 6

7

You are generating one number then printing that out several times.

Generate several numbers instead:

print "Your lucky numbers are: ", randint(1,50), randint(1,50), randint(1,50), randint(1,50), randint(1,50)

or generate a list:

numbers = [randint(1,50) for _ in range(5)]
print "Your lucky numbers are: ", ' '.join(numbers)

or better still, generate all permissible numbers (using range() then pick a sample from that:

possible_numbers = range(1, 51)  # end point is not included
numbers = random.sample(possible_numbers, 5)
print "Your lucky numbers are: ", ' '.join(map(str, numbers))

Now numbers is guaranteed to consist of entirely unique numbers.

The numbers variable does not magically update every time you print it because it refers only to the result of random.randint(1, 50).

Sign up to request clarification or add additional context in comments.

4 Comments

@AswinMurugesh: The biggest problem for the OP is the concept of what a variable refers to here. Solving the uniqueness problem is the next step.
Ok. But OP has mentioned in the question that the same numbers should not repeat
@AswinMurugesh: yes, indeed, that's why my answer includes information about how to do that. But you cannot just throw code at someone that is still coming to grips with fundamental concepts.
After all the elaborations, this is a much better answer than the accepted one.
6

Set up a set of numbers then shuffle and slice the set.

from random import shuffle
numbers  = list(range(1,51))
shuffle(numbers)
draw = numbers[:6]
print(draw)

6 Comments

shuffle modifies the list in-place and returns None.
This is not including 50, randint(1, 50) includes the endpoint in the possible values.
And in python3, shuffle does not work with iterable objects… edited with list(range(1, 50))
Thanks for the edits, a much improved answer over my original. I've clearly been using other Heathen languages for too long as I found the fact that shuffle is in-place rather than returning a new shuffled set surprising, but you learn every day.
Note that random.sample() works great with range() in Python 3, or xrange() in Python 2, because it implements __len__ and __getitem__ (it is a proper sequence object).
|
1

numbers = randint(1,50) assigns one random number to a variable. And you repeatedly use this one random number. Same goes for stars

Try this instead:

print "Your lucky numbers are: ", randint(1,50), randint(1,50), randint(1,50), randint(1,50), randint(1,50) 

Or you can create a list of numbers and get a random sample:

numbers = range(1,50)
print "Your lucky numbers are: ", ' '.join(map(str, random.sample(numbers, 5)))

Comments

0

You should call randint 5 times for your lucky numbers. You only call it once, and display the result 5 times.

Comments

0

You can use
random.sample(range(range_to_which_random_be_generated), number_of_numbers_to_generate)

example
random.sample(range(50),6)
would generate 6 different numbers in range 0 to 49

1 Comment

No, this generates numbers between 0 and 49, but the principle is otherwise sound.
0

One way i would suggest is to put the generated numbers in a list, and check if the next number is not in the list list before adding it to the list

numbers=[randint(1,50)]
i=1
while i<n:  # n is the number of random numbers you want to generate
    x=randint(1,50)
    if not x in numbers:
        i+=1
        numbers.append(x)

The problem in your code is that you generate only a single random number and print it four times

Comments

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.