View previous topic :: View next topic |
Author |
Message |
sheelu Beginner
Joined: 07 Apr 2014 Posts: 26 Topics: 6
|
Posted: Thu Jun 05, 2014 12:10 pm Post subject: Random String Generator |
|
|
Hi
Is it possible to generate random strings in COBOL?I want to generate random number and random string in COBOL so as to insert into different columns of a table.Is it possible to select a random string from an array using random function?Please provide your valuable inputs since I am totally new to random function. |
|
Back to top |
|
 |
William Collins Supermod
Joined: 03 Jun 2012 Posts: 437 Topics: 0
|
Posted: Thu Jun 05, 2014 12:33 pm Post subject: |
|
|
Yes. FUNCTION RANDOM gets you a pseudo-random number as a small floating-point value. You can then "normalise" that (turn it into some useful/necessary format) to access a COBOL table (what you are calling an array) containing your values.
Look at the Enterprise COBOL Language Reference and the Programming Guide for same. |
|
Back to top |
|
 |
sheelu Beginner
Joined: 07 Apr 2014 Posts: 26 Topics: 6
|
Posted: Fri Jun 06, 2014 5:45 am Post subject: |
|
|
Actually I want to insert a random string into db2 table every time the cobol program is executed.I tried to generate random string from below table:
01 STR-TABLE VALUE IS "ABCDEFGHIJKLMNOPQRSTUVWXYZ".
05 STR-VALUE PIC X(1) OCCURS 26 TIMES.
But I am not able to get the logic for building a random unique string from this array.The string can be of any length.(Maximum 20). |
|
Back to top |
|
 |
NASCAR9 Intermediate
Joined: 08 Oct 2004 Posts: 274 Topics: 52 Location: California
|
Posted: Fri Jun 06, 2014 11:25 am Post subject: |
|
|
I think what William is telling you is DB2 will create the Random value for you. I have used the Function a few times and it work GREAT! _________________ Thanks,
NASCAR9 |
|
Back to top |
|
 |
kolusu Site Admin

Joined: 26 Nov 2002 Posts: 12378 Topics: 75 Location: San Jose
|
Posted: Fri Jun 06, 2014 1:04 pm Post subject: |
|
|
sheelu wrote: | Actually I want to insert a random string into db2 table every time the cobol program is executed.I tried to generate random string from below table:
01 STR-TABLE VALUE IS "ABCDEFGHIJKLMNOPQRSTUVWXYZ".
05 STR-VALUE PIC X(1) OCCURS 26 TIMES.
But I am not able to get the logic for building a random unique string from this array.The string can be of any length.(Maximum 20). |
Sheelu,
Did read what William suggested? If not please follow the below suggested steps.
1. Click on "Quick Manuals" link on top of this page.
2. Click on " Enterprise COBOL Language Reference"
3. Click on
4. In the search box type "Random" without quotes and hit Search
5. Read upon the Result 3.
Once you have read and understood the function then it is a simple programming exercise. A Random is a NUMERIC value and NOT an Alphabetic String. Use the COBOL function called RANDOM to generate a random number of 18 digits. Now if your intention is to generate a 20 byte random alphabetic String, then assign value to the alphabets (A - Z = 1 thru 26) and move the values to output string.
It is a Simple programming exercise of less than 20 lines of code.
nascar9 wrote: | I think what William is telling you is DB2 will create the Random value for you. I have used the Function a few times and it work GREAT! |
Nah. William was referring to the COBOL RANDOM function. _________________ Kolusu
www.linkedin.com/in/kolusu |
|
Back to top |
|
 |
sheelu Beginner
Joined: 07 Apr 2014 Posts: 26 Topics: 6
|
Posted: Sun Jun 08, 2014 9:41 am Post subject: |
|
|
Kolusu
Thanks for the link.But I have doubts in the implementation.I tried the following to generate 5 digit random number and convert that to a string of 5 characters.
1.Generate 5 digit random number (between 1 and 99999)
rand-num = function random(1) * 99999 + 1
2How to convert the random number to corresponding string?I thought of using INSPECT verb but if I use that I will have to restrict strings between A-J.
Can you please help me out. |
|
Back to top |
|
 |
Terry_Heinze Supermod
Joined: 31 May 2004 Posts: 391 Topics: 4 Location: Richfield, MN, USA
|
Posted: Mon Jun 09, 2014 10:44 am Post subject: |
|
|
Since you are limited to digits 0-9 for each digit of your 5-digit number, why is it a problem to limit yourself to the 1st 10 characters of the alphabet for the individual characters? or the next 10 (K-T). Or ANY 10 of the 26 letters to choose from? _________________ ....Terry |
|
Back to top |
|
 |
kolusu Site Admin

Joined: 26 Nov 2002 Posts: 12378 Topics: 75 Location: San Jose
|
Posted: Mon Jun 09, 2014 11:23 am Post subject: |
|
|
sheelu,
sheelu wrote: | How to convert the random number to corresponding string?I thought of using INSPECT verb but if I use that I will have to restrict strings between A-J. |
Sheelu,
One of the easiest way to learn is to READ thoroughly the suggestions/hints and of course the manuals.
kolusu wrote: | Now if your intention is to generate a 20 byte random alphabetic String, then assign value to the alphabets (A - Z = 1 thru 26) and move the values to output string. |
Lets start thinking analytically as to how you can solve the problem. Start writing down on a piece of paper as to how you would solve this problem.
Q. How do I generate an alphabetic random string of 20 bytes.
A. Use the cobol function RANDOM and generate a 20 byte numeric value and then assign values to to the alphabets (A - Z = 1 thru 26)
Q. But how can I assign values beyond J? as ABCDEFGHIJ = 1234567890 and all the numbers are used.
A. Read the statement carefully again. If Z= 26 and K= 11 then it takes 2 digits so A = 01 and B= 02 C=03 ....
Q. oh that's good, so if I need a string of 20 bytes then I would need a random number of 40 bytes. But the maximum random number is only 18 digits.
A. That is true for ARITH(COMPAT) compiler option, but with ARITH(EXTEND) compiler option, the maximum size of each operand is 31 decimal digits.
Q. But 31 is still short of 40 bytes I need.
A. This is where you need to improvise. Generate a 20 byte random number and and use the same number in REVERSE and put in bytes 21 thru 40.
Q. Reverse? How do I do that?
A. COBOL has another function called REVERSE which you can use to reverse a String.
Q. I get it now but then what happens if the 2 digit number is 00 or greater than 26?
A. Good Question. That is what programming is all about. Replace 00 with a random number using INSPECT from 1 thru 99 using your favorite Random number function. For numbers greater than 26 and less than 100 there are tons of ways to handle it.
1. Start over the array of A thru Z in multiple of 26. ie. A=01 Z=26 and A=27 and Z= 52... If lower case letters are allowed you got another 26 alphabets. If Special characters are allowed you got another 10-15 characters depending on what you want to use.
2. Divide it by a fixed number say 33 and use the remainder if the quotient happens to be zero.
3. If you want to get fancy then divide it by another random number between 2 and 99. Same rule as above check the Quotient and use the remainder if it is zero or negative.
Q. I think I now know how to generate a random string.
A. Good. Show us the code and may be we can fine tune it. Good luck _________________ Kolusu
www.linkedin.com/in/kolusu |
|
Back to top |
|
 |
sheelu Beginner
Joined: 07 Apr 2014 Posts: 26 Topics: 6
|
Posted: Tue Jun 10, 2014 10:43 am Post subject: |
|
|
Thanks a lot Kolusu |
|
Back to top |
|
 |
sheelu Beginner
Joined: 07 Apr 2014 Posts: 26 Topics: 6
|
Posted: Tue Jun 10, 2014 10:43 am Post subject: |
|
|
I will definitely try and get back
 |
|
Back to top |
|
 |
|
|