tags:

views:

45

answers:

1

Hi there, I'm looking to trim a randomly generated number down to a whole number.

I've tried various means but none seem to work. My latest attempt is as follows:

def som = Math.random() * totalContacts
log.info som 
som.toInteger()
log.info som
def cleaned = parseInt(som)
log.info cleaned 

(I'm logging to the console after each step just to check my results. )

I get the following error when I execute the above code -

groovy.lang.MissingMethodException: No signature of method: Script69.parseInt() is applicable for argument types: (java.lang.Double) values: [44.405365593296] Possible solutions: print(java.lang.Object), print(java.io.PrintWriter), print(java.lang.Object)

Thanks, Richard

+1  A: 

The problem seems to be that on this line

def cleaned = parseInt(som)

som is a Double and there's no parseInt method that takes a Double argument. You haven't shown your imports, but I guess you've statically imported Integer.parseInt and are trying to call that.

The following change should work:

def som = Math.random() * totalContacts
log.info som 
som.toInteger()
log.info som
def cleaned = som.toInteger()
log.info cleaned 

However, it seems like what you're trying to do here is generate a random integer in the range 0..totalContacts (both inclusive). If so, the following is a simpler solution:

import org.apache.commons.lang.math.RandomUtils
import java.util.Random

Integer som = RandomUtils.nextInt(new Random(), totalContacts + 1)  
Don
Hi there I ended up getting this to work:def som = Math.random() * totalContactsrandomSeed = som.toInteger()log.info randomSeedBut will try your reponse also! Cheers!
richfort