0% found this document useful (0 votes)
5 views3 pages

Generate Random Integers in Java

Uploaded by

mnuha513
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views3 pages

Generate Random Integers in Java

Uploaded by

mnuha513
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

CPCS202 Fall 2022

How to generate random integer within a range min to max

(a) Generate random integer num where min ≤ num < max

int num = min + (int) ( [Link]() * ( max - min ) );

End-range (max) is not included

Example1

Generate random integer num where -20 ≤ num < 20 →-20 included but 20 is not included →the
maximum number generated is 19, minimum number is -20 .

Solution : int num = -20+ (int) ( [Link]() * ( 20 – (- 20) ) );

= -20+ (int) ( [Link]() * ( 20 + 20) ) );

= -20+ (int) ( [Link]() * ( 40) ) );

Example2

Generate random integer num where 10 ≤ num < 15 → 10 included but 15 is not included →the
maximum number generated is 14, minimum number is 10 .

Solution : int num = 10+ (int) ( [Link]() * ( 15-10 ) );

= 10+ (int) ( [Link]() * 5 ) );

Example3

Generate random integer num where 0 ≤ num < 100 → 0 included but 100 is not included →the
maximum number generated is 99, minimum number is 0 .

Solution : int num = 0+ (int) ( [Link]() * ( 100-0 ) );

= (int) ( [Link]() * 100 ) );

***************************************************************************

(b) Generate random integer num where min ≤ num ≤ max

int num = min + (int) ( [Link]() * ( (max + 1) - min ) );

Both min and max are included

Example1

Generate random integer num where -20 ≤ num ≤ 20 → -20 and 20 both included →the maximum
number generated is 20, minimum number is -20 .

Solution : int num = -20+ (int) ( [Link]() * ( (20 +1) – (- 20) ) );

= -20+ (int) ( [Link]() * ( 21 + 20) ) );

= -20+ (int) ( [Link]() * ( 41) ) );

Example2
Generate random integer num where 10 ≤ num ≤ 15 → 10 and 15 both are included →the maximum
number generated is 15, minimum number is 10 .

Solution : int num = 10+ (int) ( [Link]() * ( (15 +1)-10 ) );

= 10+ (int) ( [Link]() * 6 ) );

Example3

Generate random integer num where 0 ≤ num < 100 → 0 and 100 both are included →the maximum
number generated is 100, minimum number is 0 .

Solution : int num = 0+ (int) ( [Link]() * ( (100 +1)-0 ) );

= (int) ( [Link]() * 101 ) );

(c) Generate random integer num where min < num ≤ max

int num = (min + 1) + (int) ( [Link]() * ( (max + 1) - (min + 1) ) );

Start-range (min) is not included

Example1

Generate random integer num where -20 < num ≤ 20 → 20 is included but -20 is not included →the
maximum number generated is 20, minimum number is -19 .

Solution : int num = (-20 + 1) + (int) ( [Link]() * ( (20 + 1) - (-20 + 1) ) );

= -19+ (int) ( [Link]() * ( 21 + 19) ) );

= -19+ (int) ( [Link]() * ( 40) ) );

Example2

Generate random integer num where 10 < num ≤ 15 → 15 is included but 10 is not included →the
maximum number generated is 15, minimum number is 11 .

Solution : : int num = (10 + 1) + (int) ( [Link]() * ( (15 + 1) - (10 + 1) ) );

= 11+ (int) ( [Link]() * ( 16 - 11) ) );

= -19+ (int) ( [Link]() * ( 5) ) );

Example3

Generate random integer num where 0 < num ≤ 100 → 100 is included but 0 is not included →the
maximum number generated is 100 minimum number is 1 .

Solution : int num = 1+ (int) ( [Link]() * ( (100 +1)-(0+1 ) );

int num = 1+ (int) ( [Link]() * ( (101-1 ) );

= 1+ (int) ( [Link]() * 100 ) );

**********************************************************************************
(d) Generate random integer num where min < num < max

int num = (min + 1) + (int) ( [Link]() * ( max - (min + 1) ) ) ;

Both min and max are not included

Example1

Generate random integer num where -20 < num < 20 → -20 and 20 both are not included →the
maximum number generated is 19, minimum number is -19 .

Solution int num = (-20 + 1) + (int) ( [Link]() * ( 20 - (-20 + 1) ) ) ;

= -19+ (int) ( [Link]() * ( 20 + 19) ) );

= -19+ (int) ( [Link]() * ( 39) ) );

Example2

Generate random integer num where 10 < num <15 → 10 and 15 both are not included →the
maximum number generated is 14, minimum number is 11 .

Solution : : int num = (10 + 1) + (int) ( [Link]() * ( (15 + 1) - (10 + 1) ) );

= 11+ (int) ( [Link]() * ( 16 - 11) ) );

= 11+ (int) ( [Link]() * 4 ) );

Example3

Generate random integer num where 0 < num < 100 → 0 and 100 both are not included →the
maximum number generated is 99, minimum number is 1 .

Solution : (0 + 1) + (int) ( [Link]() * ( (100 + 1) - (0 + 1) ) );

= 1+ (int) ( [Link]() * ( 101 - 1) ) );

= 1+ (int) ( [Link]() * 100 ) );

Best of luck

CPCS 202 Instructors Fall 2022 , the examples added by dr. Suhelah Sandokji

You might also like