IBM Mainframe Forum Index
 
Log In
 
IBM Mainframe Forum Index Mainframe: Search IBM Mainframe Forum: FAQ Register
 

Generate random number from range of numbers in Cobol


IBM Mainframe Forums -> COBOL Programming
Post new topic   Reply to topic
View previous topic :: View next topic  
Author Message
Venkata Ramayya

New User


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

PostPosted: Fri Dec 01, 2023 3:02 am
Reply with quote

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
Back to top
View user's profile Send private message
Pedro

Global Moderator


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

PostPosted: Fri Dec 01, 2023 5:24 am
Reply with quote

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.
Back to top
View user's profile Send private message
sergeyken

Senior Member


Joined: 29 Apr 2008
Posts: 2023
Location: USA

PostPosted: Fri Dec 01, 2023 6:11 pm
Reply with quote

Pedro wrote:

and assuming the range of a generated random number is something like 0 thru 999 (one thousand possibilities)


RANDOM
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)
Back to top
View user's profile Send private message
Venkata Ramayya

New User


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

PostPosted: Mon Dec 04, 2023 10:59 pm
Reply with quote

Thanks for the response Pedro and sergeyken
Back to top
View user's profile Send private message
View previous topic :: :: View next topic  
Post new topic   Reply to topic View Bookmarks
All times are GMT + 6 Hours
Forum Index -> COBOL Programming

 


Similar Topics
Topic Forum Replies
No new posts Replace each space in cobol string wi... COBOL Programming 3
No new posts Extracting Variable decimal numbers f... DFSORT/ICETOOL 17
No new posts COBOL -Linkage Section-Case Sensitive COBOL Programming 1
No new posts To get the count of rows for every 1 ... DB2 3
No new posts COBOL ZOS Web Enablement Toolkit HTTP... COBOL Programming 0
Search our Forums:

Back to Top