lundi 7 octobre 2019

How to scrub date of birth in java in a random way which results in same random number generated when i feed the same original date of birth

I am trying to do data scrubbing, where I am trying to scrub date of birth field, but I want it to be consistent in a way, that the same random number or date of birth be generated for the same input date. Kindly help me regarding this.

I have tried this random generation code, but it generates different code, even if I provide the same input. I want the random output to remain consistent.

import java.util.GregorianCalendar;

public class RandomDateOfBirth {

    public static void main(String[] args) {
        GregorianCalendar gc = new GregorianCalendar();
        int year = randBetween(1900, 2010);
        gc.set(gc.YEAR, year);
        int dayOfYear = randBetween(1, gc.getActualMaximum(gc.DAY_OF_YEAR));
        gc.set(gc.DAY_OF_YEAR, dayOfYear);
        System.out.println(gc.get(gc.YEAR) + "-" + (gc.get(gc.MONTH) + 1) + "-" + gc.get(gc.DAY_OF_MONTH));
    }

    public static int randBetween(int start, int end) {
        return start + (int) Math.round(Math.random() * (end - start));
    }

}



Aucun commentaire:

Enregistrer un commentaire