Random Float with IronPython

DavidDavid Posts: 2
edited August 1, 2015 7:46AM in SQL Data Generator
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

  • I get this exact same message any time I try and use the random object. Has anybody figured this one out?
  • Seems the python script actually uses .Net so you can use the .Net random object, at least I could not get the python random class to work.

    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.
Sign In or Register to comment.