Back to SDS/2 Parametric Scripts

 

# generate a random number of variable length

# format integer for thousands display

 

import random

 

def int_format(s):

    if len(s)<=3:

        return s

    else:

        outList = [str(s[max(0,i-3):i]) for i in range(len(s), -1, -3)]

        outList.reverse()

        return ','.join([x for x in outList if len(x) > 0])

 

'''

def int_format(s):

    if len(s)<=3:

        return s

    else:

        if len(s) %3 == 0:

            return ','.join([str(s[i:i+3]) for i in range(0, len(s), 3)])

        return ','.join([str(s[max(0,i-3):i]) for i in range(len(s)%3, len(s)+3, 3)])

'''

 

'''

def int_format(s):

    if len(s) <= 3:

        return s

    return int_format(s[:-3]) + "," + s[-3:]

'''

 

def varRandom(dList, n):

    num = ''.join([str(random.choice(dList)) for i in range(n)])

    while num.startswith('0'):

        num = num[1:]

        num += str(random.choice(dList))

    return int(num)

 

print

print random.sample([0,1,2,3,4,5,6,7,8,9], 9)

print

 

i = varRandom([0,1,2,3,4,5,6,7,8,9], 32)

print i

print int_format(str(i))

 

print int_format('12345678923')

print int_format('1234567892')

print int_format('123456789')

print int_format('12345678')

 

'''

>>>

[9, 3, 8, 6, 5, 1, 4, 0, 7]

 

46010601035631449157303537548986

46,010,601,035,631,449,157,303,537,548,986

12,345,678,923

1,234,567,892

123,456,789

12,345,678

>>>

'''