Generate Random AlphaNumeric string in Java and RPGLE

Sometimes there is need for unique Random AlphaNumeric string to be used in applications where you have to generate a sessionId, unique file names, etc. Here is sample code both in Java and then in RPGLE for IBM i programmers to use.

You can add the string generated here with current Date and Time also !
1
2
3
$Random_       = %char(%Date():*ISO0) + '_' +      
                 %char(%Time():*HMS0) + '_' +      
                 $Random_String;     

Sample Java source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package com.mysample;
 
import java.math.BigInteger;
import java.security.SecureRandom;
 
public class GenerateRandomString {
 
    public String getString() {
 
        SecureRandom random = new SecureRandom();
        String randomString = new BigInteger(130, random).toString(32);
        return(randomString);
    }
 
}

Sample RPGLE source code for Random AlphaNumeric String

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
h DftActGrp(*no) ActGrp(*Caller)                                     
                                                                       
d ExcCmd          PR                  ExtPgm('QCMDEXC')              
d  Cmd_Str                     512a   Options(*VARSIZE)              
d                                     Const                          
d  Cmd_Len                      15p 5 Const                          
                                                                       
d $CmdStr         s            512a   inz                            
d $Random_String  s             32a                                  
d @Random_String  s               o   Class(*JAVA:'java.lang.String')
                                                                       
 * Prototype for Java String Object                                  
d crtString       PR              o   EXTPROC(*JAVA:                 
d                                             'java.lang.String':    
d                                             *CONSTRUCTOR)          
d RPGBytes                    5000A   Const Varying                  
                                                                       
 * Prototype for Java String's getBytes method                       
d cvtToBytes      PR            50A   EXTPROC(*JAVA:                 
d                                             'java.lang.String':    
d                                             'getBytes')            
d                                     Varying                        
                                                                       
* Prototype for GenerateRandomString Object                              
d GenRandom       PR              o   EXTPROC(*JAVA:                      
d                                     'com.mysample.GenerateRandomString':
d                                      *CONSTRUCTOR)                      
                                                                            
 * GenerateRandomString Object                                            
d GenRandom_      s               o   Class(*JAVA:                        
d                                     'com.mysample.GenerateRandomString')
                                                                            
 * Prototype for GenerateRandomString's getString Method                  
d getString       PR              o   EXTPROC(*JAVA:                      
d                                     'com.mysample.GenerateRandomString':
d                                     'getString')                        
d                                     Class(*JAVA:'java.lang.String')     
 *                                                                        
 *----- Main Routine                                                      
 *                                                                        
 /free                                                                    
                                                                            
      GenRandom_ = GenRandom();                                           
      @Random_String = getString(GenRandom_);                             
      $Random_String = cvtToBytes(@Random_String);                        
      dsply $Random_String;                                              
                                                                       
    *inlr = *on;                                                     
    return;                                                          
                                                                       
 /end-free                                                           
                                                                       
 *                                                                   
 *----- Initial Routine                                              
 *                                                                   
c     *inzsr        begsr                                            
                                                                       
c     *entry        plist                                            
c                   parm                    $Random_String           
                                                                       
c                   eval      $CmdStr = 'CD DIR(''/directory/folder'')'
c                   callp     ExcCmd(%Trim($CmdStr) :                
c                                   %Len(%Trim($CmdStr)))            
                                                                       
c                   endsr 

Recommended Reading

No comments:

Post a Comment

NO JUNK, Please try to keep this clean and related to the topic at hand.
Comments are for users to ask questions, collaborate or improve on existing.