Random Float with IronPython
David
Posts: 2
I want to be able to generate a random float with an IronPython script. However, it seems that I cannot use the Python random library without receiving the following error:
An error occurred. Python error at line: 152: TypeError: sequence item 0: expected bytes or byte array, str found
import random def main(config): return random.uniform(0.1, 100)
Comments
Here's what I did to generate some dummy name value pairs. "name" is a column in my table that contains "decval", "stringval","intval" or "dateval", this determines the type of value I will generate decimal, string, int and date.
#imports the CLR .net random class
import clr
clr.AddReference("System")
from System import Random
#python datetime
import datetime
# create an instance of the random class
random = Random()
def main(config):
if name == "decval":
return random.Next(100000) + random.NextDouble() # create a decimal (int + float)
elif name == "dateval":
date_1 = datetime.datetime.strptime("01/01/1900", "%m/%d/%y")
return date_1 + datetime.timedelta(random.Next(30000,42000))
elif name =="stringval":
myList =
return myList[random.Next(0,5)]
elif name =="intval":
return random.Next()
else: return "foobar"
hope it helps.