Submitted By: Justin G.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
/* Generate Random Int Within Given Range Example This Java example shows how to generate an int randomly within given range using Math class. */ public class GenerateRandomIntByRange { public static void main(String args[]){ /* The "(int)" parses the double to an int value replace 10 with your range of number. If you want a number between 5 and 15 the range is 10 [15-5] replace the 5 with the staring number. If you want the lowest number in the range to be 5 then add 5. Example : int ran = (int)(Math.random()*100)-50; will return a value in the range [-50;50] */ int random = (int)(Math.random()* 10 ) + 5; System.out.println(random); } } |
public class Account
{
private double balance; //instance varible
public Account (double initialBalance)
{
if(initialBalance>0.0)
balance = initialBalance;
}
public void credit (double amount)
{
balance = balance + amount;
}
public double getBalance()
{
return balance;
}
}
help
output is
1
1 2 1
1 2 3 2 1
1 2 3 4 3 2 1
1 2 3 4 5 4 3 2 1
If I want to generate the random integers between the range 10 to 20 ,then too I have to follow the same procedure