Saturday, October 13, 2018

Can you generate random specific digit number in java?

Problem statement: Write an algorithm to generate random specific digit ( meaning 17 digit or 18 digit or 5 digit or any number of digit) number in java.
import java.util.Random;
public class GenerateRandomNumber {
    public static void main(String args[]) {
        randomNumber();
    }
    private static void randomNumber() {
        for (int count = 0; count < 10; count++) {
            Random r = new Random();
            final float aFloat = r.nextFloat();
            final double pow = Math.pow(10, 17);
            final long round = Math.round(aFloat * pow);
            System.out.println(round);
        }
    }
}
Output:
12574732303619384
50630849599838256
37673717737197872
15234947204590
16874015331268310
39088690280914304
70018148422241208
57346004247665408
25875741243362428

84610998630523680

No comments:

Post a Comment

Blueprint for self-improvement

To learn faster: Make the process fun To understand yourself : Write To understand the world better : Read To build deeper connection : Lis...