class SmallSample


var _random = Random()

def randomString(length as int, alphabet as String) as String

require

length > 0
alphabet <> ''

ensure

result.length == length

test

utils = SmallSample()
assert utils.randomString(5, 'ab').length == 5
s = utils.randomString(1000, 'a')
for c in s, assert c == 'a'

body

sb = StringBuilder()
for i in length

c = alphabet[_random.next(alphabet.length)]
sb.append(c)

return sb.toString


def main

alphabet = 'abcdefghijklmnopqrstuvwxyz'
for i in 10, print .randomString(10, alphabet)

  • Clean, high-level syntax
  • Static and dynamic binding
  • First class support for unit tests and contracts
  • Compiled performance with scripting conveniences
  • Lambdas and closures
  • Extensions and mixins
  • ... and more


Windows, Mac OS, Linux

Download Cobra