Home > database >  how to generate multiple lines of a random list of numbers?
how to generate multiple lines of a random list of numbers?

Time:11-06

Noob python learner here.. maybe just ambitious but full immersion is my way to die. But I've asked google my question 15 different ways and not finding the answer.

I've started out trying to create a pseudolist of lottery numbers. My wife's 42nd birthday is coming up and she wants to play the Powerball and/or Mega Millions. So of course, how else to learn python than to create 42 lines of randomly picked numbers in a preset range?

[Edited:] Here's what I've got so far:


### LOTTERY NUMBERS GENERATOR ver1.0
import random
lottery = input("Do you want to win the lottery? ")
if lottery.lower() != 'yes':
    quit()
for i in range(0, 43):
    list1 = random.sample(range(0, 70), 5) # 5 quickpick numbers between 1 and 69
    list2 = random.randint(1, 26) # 1 quickpick number between 1 and 26
print("Here are your winning lottery numbers...\n " "Ticket # "   str(i), str(list1)   " and PB "   str(list2)) ### Do this 42 times
print("GOOD LUCK!")
Currently Outputs:

Do you want to win the lottery? yes
Here are your winning lottery numbers...
 Ticket #42 [22, 20, 53, 44, 29] and PB 5
GOOD LUCK!
**I'd like for it to look like this:**

Here are your winning lottery numbers:
    
    Ticket #1 - [32, 66, 29, 17, 40] and PB 13
    Ticket #2 - [59, 2, 60, ...etc]
    Ticket #3 - etc...
    ...
    Ticket #42 - [xx, xx, xx, xx, xx] and PB xx

GOOD LUCK!

That's great, but everything I've tried, it spits out the same exact set of random numbers 42 times or only one line. I want 42 DIFFERENT sets. Obviously, I know I shouldn't c&p the randomizer 42 times... so how am I supposed to write out 'do this x amount of times'?

Thanks in advance!

I've tried a couple suggestions; for loop and iteration, but apparently I'm missing some understanding.

CodePudding user response:

How are are you running this code? What are you running it on?

The code as listed should produce different results each run!

You don't have to use random.seed as some have suggested, in fact doing so can make the sequence of random numbers generated repeat on each run - which is what you don't want.

If you are planning to buy 42 lines each with different random numbers you could try this..

import random

for i in range(1,43):

    list1 = random.sample(range(1, 69), 5)
    list2 = random.randint(1, 26)

    print("Here are your winning numbers for line "   str(i)   ": "   str(list1)   " and PB "   str(list2))

print("GOOD LUCK!")

This will print out 42 sets of numbers in one go.

CodePudding user response:

You could use uuid

import uuid

print(uuid.uuid4()) # Output: 5d80dd85-da4a-4de1-8fe5-3069bbfd99ee
  • Related