import array
import os
import random


class YobaRandom:
    SIZE_LIMIT = 640 * 1024

    def __init__(self, file):
        fsize = os.path.getsize(file)
        self.bigfile = fsize > self.SIZE_LIMIT
        fh = open(file, encoding='utf=8', newline='')
        if self.bigfile:
            arr = array.array('L')
            offset = 0
            for line in fh:
                arr.append(offset)
                offset += len(line)
            self.fh = fh
            self.indexes = arr
        else:
            self.values = [val.rstrip() for val in fh]
            fh.close()

    def __del__(self):
        if self.bigfile:
            self.fh.close()

    def choice(self):
        if self.bigfile:
            idx = random.randint(0, len(self.indexes) - 1)
            offset = self.indexes[idx]
            self.fh.seek(offset)
            return self.fh.readline().rstrip()
        else:
            return random.choice(self.values)


yr = YobaRandom('list.txt')
print(yr.choice())
