Before we go to it, look at the code below. It generates 100 random numbers between 0 and 50 using randomObject.nextInt(int x) function.
Note that the the range of numbers generated is inclusive of 0 but exclusive of 50 (upper limit).
Now, let us modify this program to generate random numbers between two values. What if we want to generate numbers between two ranges? Let us say range is from 100 to 1000.
We will use something like x = MINVAL + RandomObj.nextInt(MAXVAL - MINVAL)
Analyze this code:
Note that all numbers are between 100(inclusive) and 1000(exclusive). This is pretty much what we wished to achieve.
| Advertisement |

No comments:
Post a Comment