Get in touch!Get in touch!

generate n random numbers between two values

python

The Python code below will generate n random numbers between two float values x1 and x2.

 

#!/usr/bin/python

#Randomize1.py
#Author: Victor Miti <http://umusebo.com>
#Tested on python 2.7

'''-----------------------------Description--------------------------------'''
#This python script generates the specified number of random numbers
#between two float values and writes them to a text file in the
#script's directory
'''------------------------------------------------------------------------'''

import sys, random, datetime

#get the current time
now = datetime.datetime.now()

#redirect stdout to a .txt file in the same directory
#we want the filename to have the date & time of creation
sys.stdout = open("FileName_"+now.strftime("%Y%m%d_%H%M")+".txt", "w")

#How many random numbers do you need? (specify a range, say:
#range(1,n+1) where n is the number of random numbers you need;
#OR range(0,n) where n is the number of random numbers you need.
myRange = range(0,1000)

#generate the random numbers using a for loop according to the specified range
#use round([math],x) to round it off to x decimal places
for count in myRange:
    print round(random.uniform(60, 220),0)


Leave a Reply