1

i would like to generate random numbers and write them to a .txt file. The range is : str(random.randint(0,10))

Before it generates me a random number my code should first check the .txt file. In this text file are already written down some random numbers. If the random number already exists it should generate me a new one and add it to my .txt file.

randomTxt = './random.txt'

def checkRandomNumberExists(value):
    with open(randomTxt, 'a+') as random:
        genRandom = str(random.randint(1,10))
        if value in random:
            random.write(genRandom)
        random.write('\n')

I quess i am on the wrong way. Can anyone please help me. Thank you in advance

m1711
  • 101
  • 1
  • 8
  • 2
    Naming the open file "random" is not a good idea when you're also using the `random` module. – molbdnilo Apr 30 '20 at 10:04
  • At what rate are new random numbers generated? 100 times a second, once a minute, once a day ... ? This will influence how the code is written for efficiency. – S3DEV Apr 30 '20 at 10:04
  • How many of these random numbers do you want to generate? You can instantiate a set (from the existing file) and add the numbers that are generated to the set...afterwards you can check if a generated number is already in the set. – guscht Apr 30 '20 at 10:05
  • Does this answer your question? [How do I create a list of random numbers without duplicates?](https://stackoverflow.com/questions/9755538/how-do-i-create-a-list-of-random-numbers-without-duplicates) – simonz Apr 30 '20 at 10:10
  • @molbdnilo changed the file name – m1711 Apr 30 '20 at 10:12
  • @S3DEV it depends on the .csv files i am getting. Every time when i receive a file it should generate me a new random number. In this example it should be 5 files. – m1711 Apr 30 '20 at 10:12
  • @guscht is there any example? – m1711 Apr 30 '20 at 10:12
  • @simonz i will try it. thanks – m1711 Apr 30 '20 at 10:13
  • @m1711 Don't change the file name, change the name of the variable. – molbdnilo Apr 30 '20 at 10:16
  • @m1711 the answer below is kind of what i suggested. – guscht Apr 30 '20 at 10:16
  • Should the function generate a new number or check if one exists? The function name is misleading in my opinion. – guscht Apr 30 '20 at 10:20

2 Answers2

0

Try using a while loop inside:

randomTxt = './random.txt'
with open(randomTxt, 'a+') as file:
    text = file.read()
    genRandom = str(random.randint(1,10))
    while genRandom in text:
        genRandom = str(random.randint(1,10))
    file.write(genRandom)
    file.write('\n')

Note: Please do not name files and variables a builtin name (i.e random) because it might override the original module.

U13-Forward
  • 69,221
  • 14
  • 89
  • 114
0

I don't see any reason to use argument in function because you're generating random number and you are generating random number between 1-10, what if all number is added in the text should it add number other than 1,2,3...10, please edit you question and mention that.

Below code will check if number is present in the text file or not, it will add the number in text file if it's not present else it will print the message that number already exits.

Code

import random
lines = []
num = random.randint(0,10)
f= open("random.txt","w+")
def checkRandomNumberExists():
    with open("random.txt") as file:
        for line in file: 
            line = line.strip()
            lines.append(line)

    if str(num) not in lines:
        with open("random.txt", "a") as myfile:
            myfile.write(str(num)+'\n')
            print(str(num)+ ' is added in text file')
    else:
        print(str(num)+ ' is already exists in text file')

checkRandomNumberExists()  

Output when inserted new value

7 is added in text file

Output when value is already in text file

7 is already exists in text file
Vishal Upadhyay
  • 781
  • 1
  • 5
  • 19