0

I've been writing this code to generate 9 million random numbers which contain 9 digits.

import random
passfile=open("C:/Users/HP/Desktop/testfile.txt", "a")
for passkey in range(1,9000000):
    passkey=random.randint(100000000,1000000000)
print(passkey)
passfile.close()

How can I passively check for duplicates and delete them?

exali
  • 1
  • This line -> `passkey=random.randint(100000000,1000000000)` what is random here ? – Rajan Chauhan Jul 29 '17 at 19:57
  • @ChristianDean `passkey=random.randint(100000000,1000000000)` this line will always make passkey = 100000000 . Doesn't look random right. – Rajan Chauhan Jul 29 '17 at 20:01
  • @RajanChauhan Ah, I see we were not on the same page. When you said _"what is random"_ I thought you were asking what the variable name `random` referred to. But you were asking a rhetorically question about how his code could generate a random number. – Christian Dean Jul 29 '17 at 20:02
  • @ChristianDean Exactly! Your initial reply was cute though. :D – Rajan Chauhan Jul 29 '17 at 20:05
  • On Python 2 `range` returns a list, will use a lot of memory. Use `xrange` if you must (there are probably better ways though.) – Peter Wood Jul 29 '17 at 20:13
  • @StefanPochmann Thanks for pointing out. Time has come to change my glasses. – Rajan Chauhan Jul 29 '17 at 20:29

1 Answers1

-4

You can use a set to memorise earlier random numbers and print only if elements doesn't exist in the list and then add the number to the list for next.

Pritam Pan
  • 133
  • 7
  • 2
    This would be extremely inefficient. – miradulo Jul 29 '17 at 19:59
  • Not only that @Mitch, but this is a very common dupe. Pritam, Please avoid answering common duplicate questions. It avoids having answers spread out all over the network. – Christian Dean Jul 29 '17 at 20:00
  • @ChristianDean It's actually not a dupe considering its a file and not a list – exali Jul 29 '17 at 20:04
  • 1
    @exali It is though, because the point of the question is entirely the same. – miradulo Jul 29 '17 at 20:05
  • @Mitch Can you then type out the exact code you would use for this problem? I'm a rookie so I don't understand how can I implement the question in my program. – exali Jul 29 '17 at 20:06
  • @Mitch Exactly. In both questions the objective is to generate a list of random numbers. Regardless of extra details like having to write them to a file, use them in a GUI, etc. – Christian Dean Jul 29 '17 at 20:06
  • @Mitch can you suggest a better solution without memorising the previous values please. – Pritam Pan Jul 29 '17 at 20:07
  • The best answer IMHO is the top answer on the suggested duplicate, which is reason enough for this question to be closed as a duplicate. – miradulo Jul 29 '17 at 20:12
  • @PritamPan see **`unique_everseen`** in the [**`itertools`**](https://docs.python.org/2/library/itertools.html) [recipes](https://docs.python.org/2/library/itertools.html#recipes). It uses a [**`set`**](https://docs.python.org/2/library/stdtypes.html#set). – Peter Wood Jul 29 '17 at 20:36