on 01-15-2012 1:55 PM
Hi guys,
I have a requirement..file to jdbc...where in we have an ordernumber field coming in a file for whch we need to randomly generate a num and post that to filed in oracle database....number should start with 888 followed by 8 digits..i hv put a counter after 888 and its generating an UNIQUE number...8880000000,8880000001,8880000002...etc.
if i have a file with 10 order numbers..i am getting 10 different unique numbers which ends with 88800000010....
Now the problem is when i get a second file from source, again it starting from 0, i.e 8880000000. which shouldnt happend..becaue it is already used and posted in databse....i need to generate an unique number again which starts wiht 888..
How can i achieve this??
regards
Edited by: ayrahcaanilan on Jan 15, 2012 2:56 PM
Hi,
Try reading some Java help docs about the Random library, for instance here:
http://www.cs.geneseo.edu/~baldwin/reference/random.html
You might want to try out the following code:
import java.util.Random; //import the library
...
java.util.Calendar cal;
cal = java.util.Calendar.getInstance();
Random randomGenerator = new Random(cal.get(java.util.Calendar.MILLISECOND));
int randomInt = randomGenerator.nextInt(99999999);
The randomInt variable will be given a random number between 0 and 99 999 999 (which is your requirement - just add "888" at the beginning.
But you also need to know that no (pseudo)random number generation algorithm will give you a 100% unique numbers - there is always some probability that the number will be drawn again after some time.
Hope this helps,
Greg
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi,
do you want the sequence as 8880000000,8880000001,8880000002 ...... Then you need to store this in some table within PI server. The table will have only 1 entry which will hold the last number generated. You need RFC and ABAP code which will increment the last number stored by 1 and return you the number. This RFC you can do from UDF. You also need to ensure that once number reaches 88899999999 it will be reset to 88800000000. Thus after some time in future you will get repeated numbers.
Since number of digits is fixed to 11 therefore whatever method you use, you will get repeated number after some time or the other.
Regards
Anupam
Edited by: anupamsap on Jan 15, 2012 5:14 PM
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thank you so very much Anupam.
In my requirement, the last 8 didgits can be any UNIQUE numbers.
So, Should i go for 2nd option?
I see the below code is doing some calculation by taking some date into consideration...will it have unique numbers for multiple files and generate unique numbers for all these files at the databse?? sorry i am new to this UDF sectoin.
thanks so much once again.
Regards
Hi Anupam,
Sorry, There is a mistake..it would be a 10 digit number....888 followed by 7 UNIQUE digits....thats fine if i get this unique numbers for some time atleast...we are OK...and moreover the files are not processed regularly..
Can you please share that UDF if that suffice my requirement.
Your help would really be appreciated.
Thanks
you can use java Random class to achieve this. Refer this link for sample coding
Hi,
here is the code you can use
public static String UniqueString()
{
String s=null;
try
{
Thread.sleep(1);
java.util.Calendar cal;
String pattern = "yyDDD";
java.text.SimpleDateFormat format = new java.text.SimpleDateFormat(pattern);
String h=format.format(new java.util.Date());
cal=java.util.Calendar.getInstance();
int hour24 = cal.get(java.util.Calendar.HOUR_OF_DAY);// 0..23
int min = cal.get(java.util.Calendar.MINUTE); // 0..59
int sec = cal.get(java.util.Calendar.SECOND);// 0..59
int ms = cal.get(java.util.Calendar.MILLISECOND);// 0..999
long l=(((hour24*60+min)*60)+sec)*1000+ms;
l=(l)%100;
if(l<10)
{
l=l+10;
}
s=h+l;
s="888"+s;
}
catch(Exception e)
{
e.printStackTrace();
}
return s;
}
If you use Random class for generating numbers then there is no guarantee that subsequent numbers are unique.
Here is the output from my code
8881201579
8881201580
8881201581
8881201582
8881201583
8881201584
8881201585
8881201586
8881201587
8881201588
If you process not more than 99 files each day , this code will generate unique number each time you run this.
There is minimal chance that subsequent numbers will be same if you run this code.
Hope this solves your problem.
Alternatively you can use this UDF
public static String UniqueVal()
{
String s="888";
try
{
java.util.Calendar cal;
Thread.sleep(1);
cal=java.util.Calendar.getInstance();
long l=((new java.util.Date().getTime())+ cal.get(java.util.Calendar.MILLISECOND))%10000000;
s=s+l;
}
catch(Exception e)
{
e.printStackTrace();
}
return s;
}
output
8882082308
8882082310
8882082312
8882082314
8882082316
8882082318
8882082320
8882082322
8882082324
8882082326
8882082328
8882082330
8882082332
8882082334
8882082336
8882082338
8882082340
8882082342
8882082344
8882082346
8882082348
8882082350
8882082352
8882082354
Would suggest that you use the second UDF here in my post rather than the first one.
Regards
Anupam
Edited by: anupamsap on Jan 15, 2012 7:35 PM
Hi,
These numbers are generated by the system depending on current time(2nd UDF) and I have no control over them. Thus if you need numbers to be sequential in increasing order by 1 then follow instructions in my first post (RFC call), since here you need to store in table the last generated number. One more suggestion while you use the UDF in real time just try commenting the line Thread.sleep(1); . I did put this line since I wanted to see whether consecutive numbers are unique or not. This line is causing the even number generation. Thus if this line is removed you can get odd numbers as well. If you are not posting files every milliseconds the code should work without the line Thread.sleep(1); .
Regards
Anupam
N.B- I modified this post. Please kindly check.
Edited by: anupamsap on Jan 16, 2012 6:40 AM
User | Count |
---|---|
83 | |
10 | |
10 | |
9 | |
7 | |
6 | |
5 | |
5 | |
4 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.