Ask a Question

I want a groovy simple code that generates Random number

HimanshuTayal
Community Hero

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?

 


Click "Accept as Solution" if my answer has helped,
Remember to give "Kudos" πŸ™‚ ↓↓↓↓↓



Thanks and Regards,
Himanshu Tayal
2 REPLIES 2
msiadak
Community Hero

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. 




---

Click the Accept as Solution button if my answer has helped, and remember to give kudos where appropriate too!

Thanks for the reply @msiadak Smiley Very Happy


Click "Accept as Solution" if my answer has helped,
Remember to give "Kudos" πŸ™‚ ↓↓↓↓↓



Thanks and Regards,
Himanshu Tayal
cancel
Showing results forΒ 
Search instead forΒ 
Did you mean:Β