Showing posts with label two. Show all posts
Showing posts with label two. Show all posts

Thursday, 14 November 2013

Random integers between two values (a range) in java

Refer to : http://mycodedock.blogspot.in/2013/11/random-integers-in-java.html

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 |

Friday, 8 November 2013

Getting number of days between two dates in Java

The task is to design an application in Java7 that can read two dates in YYYY MM DD form and then compute & print the number of days between them.

The code for the application is given below


| Advertisement |