Forum Discussion

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

How to transfer some output to properties

I was find a java coding and I want to use the output value for properties transfer, however I don't know how to do. Can you teach me? Thanks

import java.util.Random;

/** Generate random integers in a certain range. */
public final class RandomRange {
 
  public static final void main(String[] args){
    log("Generating random integers in the range 1..10.");
 
    int START = 100;
    int END = 256;
    Random random = new Random();
    for (int idx = 1; idx <= 10; ++idx){
      showRandomInteger(START, END, random);
    }
   
    log("Done.");
  }
 
  private static void showRandomInteger(int aStart, int aEnd, Random aRandom){
    if ( aStart > aEnd ) {
      throw new IllegalArgumentException("Start cannot exceed End.");
    }
    //get the range, casting to long to avoid overflow problems
    long range = (long)aEnd - (long)aStart + 1;
    // compute a fraction of the range, 0 <= frac < range
    long fraction = (long)(range * aRandom.nextDouble());
    int randomNumber =  (int)(fraction + aStart);   
    log("Generated : " + randomNumber);

  }
 
  private static void log(String aMessage){
    System.out.println(aMessage);

  }
}


--------------------------------------------------

I was using following coding, but it not work.

def props = testRunner.testCase.getTestStepByName("Properties");
props.setPropertyValue("randomValue", randomNumber)

14 Replies

  • omatzura's avatar
    omatzura
    Super Contributor
    Hi,

    well, if you want it between 1 to 9, try using

    def randomNumber = 1 + (int)(Math.random()*9)
    ...

    or between 1000 to 9999, try using

    def randomNumber = 1000 + (int)(Math.random()*9000)
    ...

    etc.

    regards,

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

    Thanks so much. I was try the

    def n = (int)(10)
    def randomNumber4 =(int)(Math.pow(10,n-1))+(int)(Math.random()*9*(Math.pow(10,n-1)))
    def props = testRunner.testCase.getTestStepByName("Properties");   
    props.setPropertyValue("randomValue4", String.valueOf(randomNumber4))


    I find that if the n>=10, the random number is incorrect.  Is it exist a limit of properties.

    ..........

    def n = (int)(11)
    def randomNumber5 =(int)(Math.pow(10,n-1))
    def props = testRunner.testCase.getTestStepByName("Properties");   
    props.setPropertyValue("randomValue5", String.valueOf(randomNumber5))

    If the n is 11, the value of randomNumber5 in properties is 2147483647. How to increase the limit of these properties?
  • omatzura's avatar
    omatzura
    Super Contributor
    Hi!

    try using long or double types instead.. !?

    /Ole
    eviware.com
  • Daniel-yip's avatar
    Daniel-yip
    Occasional Contributor
    long or double types still not enoug, but I find another way to solve my problem.........anyways thank you so much for your help.