0 Replies - 4879 Views - Last Post: 29 April 2015 - 05:21 PM

#1 atraub   User is offline

  • Pythoneer
  • member icon

Reputation: 833
  • View blog
  • Posts: 2,257
  • Joined: 23-December 08

Lazily generate a file containing random numbers

Posted 29 April 2015 - 05:21 PM

Works with Python 2.7 and Python 3+. This is great for generating big sets of random values.
import random

def generateSampleDataFile(filename, size, minNum, maxNum):
    """Lazily generates a file with random numbers between a certain range

        Args:
          filename (str): The output file holding the random values
          size (int): The number of random values to generate
          minNum (int): The lowest possible random value
          maxNum (int): The highest possible ranom value
     """
    with open(filename,'w') as outFile:
        gen = ("{}\n".format(random.randint(minNum, maxNum)) for i in range(size))
        for val in gen:
            outFile.write(val)


Is This A Good Question/Topic? 0
  • +

Page 1 of 1