mardi 27 août 2019

Getting same number when using Math.random() and ThreadLocalRandom.current().nextDouble() and Random class nextDouble() method?

A BankApplication program in Java,problem in setDebitCard() function.

Here I am not able to generate a unique random number after every java run for debitCardNumber variable while it is working fine with debitCardPin

I tried working with Random class method nextDouble(),Math.random(),ThreadLocalRandom.current().nextDouble() too..

Problem 1):The debitCardNumber output is same after trying everything mentioned above.(No syntax error whatsoever)

Problem 2):Also I am getting the same 10 digit debitCardNumber instead of 12 digit.

Problem 3):debitCardPin is working fine,But Sometimes it gives me a unique 3 digit random number.

//CODE

package bankAccountApp;

import java.util.Random;

import java.util.concurrent.ThreadLocalRandom;

public class Current extends Account {

private int debitCardNumber;

private int debitCardPin;

Random rand = new Random();

//setDebitCard() code

private void setDebitCard(){

debitCardNumber = (int)(ThreadLocalRandom.current().nextDouble()*Math.pow(10,12));

/* Also tried using

debitCardNumber = (int)(rand.nextDouble()*Math.pow(10,12));

debitCardNumber = (int)(Math.random()*Math.pow(10,12));

*/

debitCardPin = (int)(Math.random()*Math.pow(10,4));
}

Expected Result:

1)debitCardNumber should return a unique random 12 digit integer after every JAVA RUN

2)debitCardPin should return only 4 digit integer.

Actual Result:

1)debitCardNumber returns same 10 digit integer after every JAVA RUN

2)debitCardPin sometimes returns a 3 digit unique random integer.




Aucun commentaire:

Enregistrer un commentaire