# Generate random number from range of numbers in Cobol

Author Message
Venkata Ramayya

New User

Joined: 03 Dec 2007
Posts: 49
Location: United States

 Posted: Fri Dec 01, 2023 3:02 am Hello, I have a requirement to generate random number from range of numbers. I know random function can be used, but how I can generate numbers between range. Can anyone share if you built similar logic before? Example: Low Range - 36 High Range - 60 I would need to get random numbers between 36 and 60
Pedro

Global Moderator

Joined: 01 Sep 2006
Posts: 2558
Location: Silicon Valley

 Posted: Fri Dec 01, 2023 5:24 am I think something like this: The range is 24 (60-36) and assuming the range of a generated random number is something like 0 thru 999 (one thousand possibilities) 1000 / 24 = 41.6666667 and x is a generated random number: (x / 41.6666667) + 36 = and then round off the result.
sergeyken

Senior Member

Joined: 29 Apr 2008
Posts: 2050
Location: USA

Posted: Fri Dec 01, 2023 6:11 pm

 Pedro wrote: and assuming the range of a generated random number is something like 0 thru 999 (one thousand possibilities)
Quote:
The RANDOM function returns a numeric value that is a pseudorandom number from a rectangular distribution.

The function type is numeric.

Format
 Code: FUNCTION RANDOM(argument-1)

argument-1

If argument-1 is specified, it must be zero or a positive integer. However, only values in the range from zero up to and including 2,147,483,645 yield a distinct sequence of pseudorandom numbers.
If a subsequent reference specifies argument-1, a new sequence of pseudorandom numbers is started.

If the first reference to this function in the run unit does not specify argument-1, the seed value used will be zero.

In each case, subsequent references without specifying argument-1 return the next number in the current sequence.

The returned value is exclusively between zero and one.

 Code: NEW-NUMBER = 36 + (60 - 36) * (FUNCTION RANDOM)

or (to include the upper value 60 as possible choice):
 Code: NEW-NUMBER = 36 + (60 - 36 + 1) * (FUNCTION RANDOM)
Venkata Ramayya

New User

Joined: 03 Dec 2007
Posts: 49
Location: United States

 Posted: Mon Dec 04, 2023 10:59 pm Thanks for the response Pedro and sergeyken
 View Bookmarks All times are GMT + 6 Hours

 Topic Forum Replies Similar Topics COBOL 6.4 - User Defined Function nee... COBOL Programming 6 Replace each space in cobol string wi... COBOL Programming 3 Extracting Variable decimal numbers f... DFSORT/ICETOOL 17 COBOL -Linkage Section-Case Sensitive COBOL Programming 1 To get the count of rows for every 1 ... DB2 3
Search our Forums:

 IBMMainframes.com is not an official and/or affiliated with IBM® in anyway Board Rules | FAQ | Downloads | Wiki | SiteMap | Contact Us