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
groovyguy
Champion Level 0

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 @groovyguy 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: