2

I'm trying to do a lottery extractor and i need a script that extracts random numbers and stores the extracted ones to not extract them again.

I came out with the idea of storing the extracted numbers in a text file (ToExclude.txt) and to use the file as "extracted" variable.

I tried doing this without success:

import random

f = open("ToExclude.txt", "r") #Opens the txt in read mode
NumToExclude = f.read() #Stores the txt file (with the previous extracted numbers)
f.close()
open("ToExclude.txt", "w").close() #Erases the content of the file

number = random.randint(1,100) #Random number generator

if number in NumToExclude: #Verifies that the number is not in the Extracted list
    [RESTART FUNCTION] #Restarts the function to generate another number
else:
    NumToExclude.append(number) #If is not in the list add the number to the lsit

print("The number is: {}".format(number)) #Example of output

f = open("ToExclude.txt", "w")  #Stores the extracted number in the txt file
f.write(str(NumToExclude))
f.close()

When i'm executing it the first one goes well, but after the 2nd the text file looks like this:

['[', '5', '7', ',', ' ', '5', '7', ']', 67, 67]
  • `random.sample(range(100),x )` will return `x` random numbers that are non-replaced. – jtweeder Nov 06 '19 at 14:43
  • random.sample(range(1,100),x) for the same numbers as in the question (from 1 till 99) – JohanC Nov 06 '19 at 14:46
  • `NumToExclude = f.read()` puts a text string into NumToExclude, but you want a list of numbers – JohanC Nov 06 '19 at 14:49
  • NumToExclude= [ast.literal_eval(line.strip()) for line in f]` as in https://stackoverflow.com/questions/33351770/python-read-a-txt-file-into-a-list-of-lists-of-numbers – JohanC Nov 06 '19 at 14:51
  • Shuffle your list of numbers and pick however many you want from the shuffled list. That guarantees no repeats provided the initial list had no repeats. – rossum Nov 06 '19 at 19:43
  • Thank you all for your advices – Tobia Giampiccolo Nov 07 '19 at 08:42

3 Answers3

1

You are overthinking.You can just do:

all_numbers = list(range(1,101))

extracted = random.choice(all_numbers)
all_numbers.remove(extracted)

If you want, you can cycle through the last 2 lines and save the values inside a list as well:

all_numbers = list(range(1,101))
result =[]
for i in range(10):
    extracted = random.choice(all_numbers)
    result.append(extracted)
    all_numbers.remove(extracted)
Miguel
  • 1,295
  • 12
  • 25
  • But i need to run the script over and over, doesn't the variable "Extracted" reset when i re-run the script? – Tobia Giampiccolo Nov 06 '19 at 14:51
  • @TobiaGiampiccolo but why do you need to rerun the script? What is the final goal? you can just extract them all in 1 go, if you want to extract them 1 by each time you run the script, yes, you need to save the data in a text file. – Miguel Nov 06 '19 at 14:52
0

There are a lot of things wrong here. Firstly, you can't just dump a list into a text file then read it back in as a list that's why you're getting odd behaviour. You should look into a module called pickle. Secondly, as the other answer which has just appears points out you don;t need all this complexity. Just start with a list of all the numbers and use random.choice() to pick from it. Thirdly, it's best to open text files in a with block as that way they will be closed properly if an exception occurs.

with f as open('path','r'):
 f.read()
Simon Notley
  • 2,070
  • 3
  • 12
  • 18
0

As you are erasing the file I assume you don't need to keep it for long term. You can use a set, is an inmutable structure, and doesn't allow repetition.

import random

lottery = set() #empty set

Let's say you want to generate 6 numbers:

while len(lottery) < 7:

    lottery.add(random.randint(1,100)) #same method you used for random

print(lottery)
{6, 41, 75, 49, 85, 87, 61}

If you need to run many times, put it into a function

def run_lottery():

    lottery = set()

        while len(lottery) < 7:
                lottery.add(random.randint(1,100)) #same method you used

    return lottery

And call it with a variable:

  lucky = run_lottery()

  print(lucky)
  {100, 10, 13, 17, 49, 57, 63}
powerPixie
  • 718
  • 9
  • 20