Forum Discussion

HimanshuTayal's avatar
HimanshuTayal
Community Hero
6 years ago

I want a groovy simple code that generates Random number

I want a groovy simple code that generates Random number, and i don't want to add random number generator

 

Currently i am using:

 

import org.apache.commons.lang.RandomStringUtils

log.info getRandomNumber(8)

def getRandomNumber(int num)
{
       String charset = (('1'..'9')).join()
       def randValue = RandomStringUtils.random(num, charset)
       while (randValue.size()!=num)
      {
              randValue = RandomStringUtils.random(num, charset)
       }
       return randValue
}

Any alternate or better solution for this?

 

2 Replies

  • groovyguy's avatar
    groovyguy
    Champion Level 1

    Short of using the built in libraries, you have a decent enough solution. Any other solution I could think of is just a variation of what you already have.