mercredi 31 octobre 2018

Random Number generating in java without having any number of 0 in it

I have to generate 5 digit random number , without having any 0 in it. I have tried with below code, sometimes it works and sometimes it doesn't Is there any better way to do this?

public  Integer generateLastFiveSequenceNumbers()
        {
            Random ranndomNumber = new Random();
            Random replaceNumber = new Random();

            Integer fiveDigitRanndomNumber = 11111 + ranndomNumber.nextInt(99999);
            Integer replaceZeroNumber = 1 + replaceNumber.nextInt(9);


            String tempValidation = fiveDigitRanndomNumber.toString();
            char[] ch = tempValidation.toCharArray();

            for(int i = 0 ; i < ch.length-1 ;i++)
            {
                if(ch[i]=='0')
                {
                    ch[i] = '1'; 

                }
            }

            String newValue = new String(ch);
            Integer finalNumber = Integer.parseInt(newValue);
            return finalNumber;
        }




Aucun commentaire:

Enregistrer un commentaire