cancel
Showing results for 
Search instead for 
Did you mean: 

UDF to generate 4-Digit Positive Random Number

Former Member
0 Kudos

Hi,

I want to generate 4-Digit Positive Random Number in the Mapping for a field. Could anyone please provide Inputs or a UDF.

Regards,

Varun

Accepted Solutions (1)

Accepted Solutions (1)

santhosh_kumarv
Active Contributor
0 Kudos

Hi,

Instead of UDF.. you can do this... Use the standard function current date which will return date and time.... pass this to the standard String function to substring and use it..

~SaNv...

Answers (5)

Answers (5)

varun_k
Contributor
0 Kudos

This is the UDF I have Used

java.util.Random randomNumber;

String TransactionId = "" ;

randomNumber = new Random();

long longNumber = randomNumber.nextLong();

if (longNumber < 0)

{

longNumber = longNumber * (-1);

}

longNumber += 10000;

TransactionId = (String.valueOf(longNumber)).substring(0, 4);

return TransactionId ;

Thanks everyone for your Inputs

Former Member
0 Kudos

you can try this also


int x = (int)(Math.random() * 9999)+1000

random()


Returns a double value with a positive sign, greater than or equal to 0.0 and less than 1.0.

Former Member
0 Kudos

use Random class from package java.util

Random:

Random is the class of java.util.*; package. This class is used to create a random number

nextInt():

This method returns a random number in random number generator's sequence. If you pass a value to the method then the method return the random number in the mentioned range otherwise it will generate the random number in sequence according to the random number generator.

Shabarish_Nair
Active Contributor
0 Kudos

try

import java.util.Random;
 Random generator = new Random();
int random_number = generator.nextInt(9999) + 1000;

former_member750652
Contributor
0 Kudos

Hi Varun,

You can use Current time and date or combination of time,date and any numeric field from payload.Define some mathematical calculation in UDF and truncate it in the UDF it self to 4 digits using substring.Even while writing UDF you need to convert the values from string to Integers and perform calculations as UDF takeas inputs as String values.

Thanks,

Ram.