Number, Generate Random

Number, Generate Random

Generates a random number. When a token reaches this automated step, a numerical value within the specified range is generated each time. The decimal part is rounded down toward 0 according to the workflow data definition.

2019-11-13 (C) Questetra, Inc. (MIT License)
https://support.questetra.com/addons/number-generate-random/

Configs
  • A: Set Lower Limit (e.g. “0”, “-0.5”) * #{EL}
  • B: Set Upper Limit (e.g. “10”, “0.5”) * #{EL}
  • C: Select NUMERIC DATA for Generated Number (update) *
Script
// (c) 2019, Questetra, Inc. (the MIT License)

//////// START "main()" ////////
main();
function main(){ 


//// == Config Retrieving / 工程コンフィグの参照 ==
const lower   = configs.get( "conf_Lower"   ) + ""; // config required
const upper   = configs.get( "conf_Upper"   ) + ""; // config required
const dataIdC = configs.get( "conf_DataIdC" ) + ""; // config required
// 'java.lang.String' (String Obj) to javascript primitive 'string'

// Design-time Config Format Check
const numLower = parseFloat( lower );
engine.log( " AutomatedTask Lower Limit: " + numLower );
const numUpper = parseFloat( upper );
engine.log( " AutomatedTask Upper Limit: " + numUpper );
if( isNaN(numLower) ){
  throw new Error( "\n AutomatedTask ConfigError:" +
                   " Config {Lower} is not a number \n" );
}
if( isNaN(numUpper) ){
  throw new Error( "\n AutomatedTask ConfigError:" +
                   " Config {Upper} is not a number \n" );
}
if( numLower > numUpper ){
  throw new Error( "\n AutomatedTask ConfigError:" +
                   " Config {Lower} is greater than {Upper} \n" );
}


//// == Data Retrieving / ワークフローデータの参照 ==
// nothing (This Service Task is a CREATE type)


//// == Calculating / 演算 ==
let numDiff  = numUpper - numLower;
let mathRand = Math.random(); // Math.random(): 0.00 to 0.99
engine.log( " AutomatedTask Math.random(): " + mathRand );
let numRand  = mathRand * numDiff + numLower;
engine.log( " AutomatedTask Generated Random: " + numRand );


//// == Data Updating / ワークフローデータへの代入 ==
engine.setDataByNumber( dataIdC, new java.math.BigDecimal( numRand ) );


} //////// END "main()" ////////

Download

Capture

Generates a random number. When a token reaches this automated step, a numerical value within the specified range is generated each time. The decimal part is rounded down toward 0 according to the workflow data definition.

Notes

  1. Setting the range specification from 0 to 10 generates a number from 0.00 to 9.99.
  2. If a random number is required as a character string, consider using “String, Generate Password“. (0 starts, etc.)
  3. The format of the lower limit and upper limit depends on the JavaScript “parseFloat()” function.

See also

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: