Forum Discussion

Daniel-yip's avatar
Daniel-yip
Occasional Contributor
17 years ago

How to generate a random number in a particular range

I want to generate a random number in a particular range, but how can write groovy script for this purpose?
  • Daniel-yip's avatar
    Daniel-yip
    Occasional Contributor
    Can I use Random class in java.util sunch as Math.random()? If I can, How to do? Thanks
  • omatzura's avatar
    omatzura
    Super Contributor
    Hi!

    sure.. try

    context.randomValue = 5 + 5*Math.random()

    to generate a value between 5 and 10!?

    and then us it in a request with

    ${randomValue}

    regards,

    /Ole
    eviware.com
  • Daniel-yip's avatar
    Daniel-yip
    Occasional Contributor
    Thanks....

    I copy the following script and the copy to groovy script for testing. A Lot of error was display.

    context.randomValue = 5 + 5*Math.random()
    def props = testRunner.testCase.getTestStepByName("Properties");
    props.setPropertyValue("randomValue", ${randomValue});

    How can I generate the the random value and transfer to property?
  • omatzura's avatar
    omatzura
    Super Contributor
    Hi!

    either use property-expansion as I showes in my initial response, or write the value to a property with

    def randomValue = 5 + 5*Math.random()
    def props = testRunner.testCase.getTestStepByName("Properties");
    props.setPropertyValue("randomValue", randomValue );

    and then use a property-transfer to transfer it to some target..

    Hope this helps!

    regards,

    /Ole
    eviware.com
  • Daniel-yip's avatar
    Daniel-yip
    Occasional Contributor
    The following error was displayed when execute your suggest code. What should I do?

    groovy.lang.MissingMethodException: No signature of method: com.eviware.soapui.impl.wsdl.teststeps.WsdlPropertiesTestStep.setPropertyValue() is applicable for argument types: (java.lang.String, java.lang.Double) values: {"randomValue", 5.3027697549785975}
  • omatzura's avatar
    omatzura
    Super Contributor
    Hi!

    try

    ...
    props.setPropertyValue("randomValue", String.valueOf(randomValue) )

    instead.

    regards,

    /Ole
    eviware.com