View previous topic :: View next topic |
Author |
Message |
nbdtrjk1 Beginner
Joined: 12 Apr 2007 Posts: 76 Topics: 41
|
Posted: Wed Sep 24, 2008 2:58 am Post subject: Random number genaration |
|
|
HI
I want to generate unique random number with in range of 10 to 20. i used below logic. I got a some of the number coming Redundant(12,13,15), but i want unique random number...
Code: | IDENTIFICATION DIVISION.
PROGRAM-ID. EXT7.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 MIN-NUMBER PIC 99 VALUE 10.
01 MAX-NUMBER PIC 99 VALUE 20.
01 RANDOM-NUMBER PIC 99.
PROCEDURE DIVISION.
MAIN-PARA.
PERFORM 10 TIMES
COMPUTE RANDOM-NUMBER = FUNCTION RANDOM *
(MAX-NUMBER - MIN-NUMBER + 1) +
MIN-NUMBER
DISPLAY 'RANDOM NUMBER:' RANDOM-NUMBER
END-PERFORM.
STOP RUN. |
Output
RANDOM NUMBER:15
RANDOM NUMBER:11
RANDOM NUMBER:19
RANDOM NUMBER:12
RANDOM NUMBER:20
RANDOM NUMBER:15
RANDOM NUMBER:13
RANDOM NUMBER:18
RANDOM NUMBER:13
RANDOM NUMBER:12
Please help me on this.
Thanks in advance |
|
Back to top |
|
|
Nic Clouston Advanced
Joined: 01 Feb 2007 Posts: 1075 Topics: 7 Location: At Home
|
Posted: Wed Sep 24, 2008 12:25 pm Post subject: |
|
|
Store each unique random number you generate. When you generate the next one check it against the unique ones already generated. If not unique, generate another.
Why do you need help - it seems so obvious - but then I have done this several times. _________________ Utility and Program control cards are NOT, repeat NOT, JCL. |
|
Back to top |
|
|
jsharon1248 Intermediate
Joined: 08 Aug 2007 Posts: 291 Topics: 2 Location: Chicago
|
Posted: Wed Sep 24, 2008 1:25 pm Post subject: |
|
|
What is random? A routine that generates a true random number has no memory of previous numbers generated. This means that sometimes, numbers in the sequence can be generated multiple times. In fact, it would be very unlikely to generate a set of 11 numbers within a range of 11 and have all the numbers different. Try rolling two dice and see how long it takes to roll them 12 times in a row with 12 different values.
Also, the COBOL RANDOM function is not a true random number generator.
Code: | For a given seed value, the sequence of pseudorandom numbers will always be the same. |
If you want the appearance of a random number generate, supply the argument to the RANDOM function in a way that you use a different number every time you run the program. Convert the date/time to a numeric value and supply that value to function so that you're not always using the same sequence. |
|
Back to top |
|
|
luckystarlak Beginner
Joined: 12 Jan 2007 Posts: 4 Topics: 1
|
Posted: Tue Dec 23, 2008 6:02 am Post subject: |
|
|
you can simply change the MIN and MAX number for every iteration to reduce unnecessary checks. hope it helps. |
|
Back to top |
|
|
semigeezer Supermod
Joined: 03 Jan 2003 Posts: 1014 Topics: 13 Location: Atlantis
|
Posted: Tue Dec 23, 2008 3:13 pm Post subject: |
|
|
depending on the number of items you have, simply checking for a used number in an array may be very expensive because to generate the last few numbers, you may have to try and try and try over and over until you get an unused number. For large sets, a better approach is sometimes to create a structure representing each possible number and instead of generating the final random number, you generate the index into the structure. When a number is used, you remove it from the structure, decrease your highest possible generated index by 1 and do it again for the next one. For a fast language like COBOL, this might be a bit of overkill, but for slower or interpreted languages like Rexx, this approach is almost required for very large sets. _________________ New members are encouraged to read the How To Ask Questions The Smart Way FAQ at http://www.catb.org/~esr/faqs/smart-questions.html. |
|
Back to top |
|
|
sriramla Beginner
Joined: 22 Feb 2003 Posts: 74 Topics: 1
|
Posted: Tue Dec 23, 2008 3:25 pm Post subject: |
|
|
I used the below approach once for a similar problem:
Lets say you want to generate 25 random numbers (from 101 to 125). Declare an array of size 25 and value each element from 101 to 125. Now all the elements in the array are in specific order. Then run a loop of 'n' iterations picking any two random elements and swap them. More the iterations means more randomness. This ensures that you will never have to worry about duplicate items, no overhead in the code etc. You can choose the value of 'n' to suit to your needs. |
|
Back to top |
|
|
|
|