Hi all ,my friend asked me this program today,so i thought of sharing it with you all.
This program will generate a random 10 digit no every time you will run
here base is taken as 1000000000
and max limit as 9999999999
Math.abs() is used to insure it give positive number
if Math.abs() is removed you can get negative number as well
In order to generate more than one number remove for loop in program
and assign desired value to the x present in for loop
long variable(start and end) both are initialized in different manner ,both style can be used to initialize a long variable in java
Hope you like this post!!!
Ask questions if any,Thanks a lot
package random;
import java.util.Random;
public class random {
public static void main(String[] args) {
long start= Integer.parseInt("1000000000");
//LONG can initializes as start(long start) as well as the next line end(long end)
long end=9999999999L;
Random random =new Random();
//for(int x=1;x<=10;x++){
//you can remove comment from for loop in order to generate multiple times
//jut assign the value to x,whatever tome you want to generate
long range =(long)end-(long)start+1;
long fraction =(long)(range*random.nextDouble());
int randomNumber=(int)(fraction+start);
System.out.println(Math.abs(randomNumber));
//Math.abs() is used in order to give positive numer
//if you will remove Math.abs() it will give positive as well as negative number
//}
}
}
No comments:
Post a Comment