Question
Commonwealth Bank of Australia
AU
Last activity: 16 Oct 2018 12:03 EDT
Best Approach for random number generation
Hi,
Suppose, I need to generate a random number specific to a range. Can I create a Rule-Utility-Function for the same or can I create a data transform implementing some logic and parameterizing the properties.
What would be best approach for the same?
If there is any OOTB function also please let me know.
Thanks and Regards,
Subhajit
**Moderation Team has archived post**
This post has been archived for educational purposes. Contents and links will no longer be updated. If you have the same/similar question, please write a new post.
-
Likes (3)
Abhijeet Kumar Vemuri V Ashwin Hariharan R Lakshmi Durga Gogireddy -
Share this page Facebook Twitter LinkedIn Email Copying... Copied!
Accepted Solution
Updated: 11 May 2017 13:53 EDT
Pegasystems Inc.
US
You could have a Data Transform set an OUT param as shown below.
Set param.IndexOut = java.util.concurrent.ThreadLocalConcurrent.nextInt(param.Start, param.Max + 1)
But from a reuse perspective a Function makes more sense since a Data Transform can call a Function that does the same thing, namely, accept two Integer parameters and return a random Integer that is >= Start and <= Max.
Commonwealth Bank of Australia
AU
Thank you so much Lee.
Pegasystems Inc.
IN
OOTB- Random number function is already available in DataLoader library. This can be re-used.
Sample Code:
Pegasystems Inc.
IN
Hi Lee,
When I used the following code in my Data Transform it is showing error.
Set param.IndexOut = java.util.concurrent.ThreadLocalConcurrent.nextInt(param.Start, param.Max +1)
Attached the screenshot for more information.
Pegasystems Inc.
IN
You may want to try like this., but function is recommended from reusability perspective.
Set param.IndexOut = @java("java.util.concurrent.ThreadLocalRandom.current().nextInt(tools.getParamAsInteger(ImmutablePropertyInfo.MODE_STRING,\"Start\"), tools.getParamAsInteger(ImmutablePropertyInfo.MODE_STRING,\"End\"))")
Allianz Germany
DE
Somehow could not get this work. Can you show show the screenshot of Data transform and parameter data types. I also tried Engine API (7.1.9) and java 1.6
in data transform but getting compilation failure
API Call used :
@java("tools.getThread().getSystemRandom().nextInt(10)")
Test compilation failed: ----------
1. ERROR in /Rule_Obj_Model_AzD_TAUtils_Work_Parent_SetConversionRate_Model_20170927T132257_619_GMT.java (at line 62)
tools.putParamValue("RandomNumber", PropertyInfo.TYPE_INTEGER, com.pega.pegarules.priv.FUAUtil.parseInt((tools.getThread().getSystemRandom().nextInt(10))));
^^^^^^^^
The method parseInt(String) in the type FUAUtil is not applicable for the arguments (int)
Pegasystems Inc.
US
Strangely enough when you use @java() need to append: + " "
If you search the PDN you will find someone having pointed this out in the past.
Allianz
DE
I think there is some issue during the data Conversion issue (most likely a bug in Pega). The fact is it should return Java of type Integer but due to some data type conversion it cannot. By adding + " " you are essetially converting the return type to String since contatenation to " " (String) converts the Object to String in java.
Pegasystems Inc.
US
Yes exactly.
Different @java() calls can return any data type - int, double, BigDecimal, Date, etc., not just Integer.
For Pega to deal with those differences the return value from @java() needs to be converted to a String.