dimanche 12 février 2017

Error with calendar class and generating numbers

I'm trying to do random number generation using the given date to create good pseudo-random numbers in Java. I decided to use the calendar class and to count milliseconds in the day. This worked to some extent, but I can only get a different random value when I start the program. Running it any more times in the program will just give me the same number. I'm using Thread.sleep to make sure that there is an actual difference on time, but I still get the same numbers. Heres my method that I'm calling (from another class)

public long genRNG()
{
    long mask = 0xFFFF000000000000L;
    long randomValue = seed & mask;
    seed = 0x5D588B656C078965L * cal.get(Calendar.MILLISECOND) + 0x0000000000269EC3;
    return randomValue;
}

and here's my main method

public static void main(String[] args) throws InterruptedException
{
       Seed key = new Seed();
        for (int x = 0; x <=10; x++)
        {
        Thread.sleep(200);
        System.out.println(key.genRNG());
        }
}

and the given output:

-7389844038561562624
-7389844038561562624
-7389844038561562624
-7389844038561562624
-7389844038561562624
-7389844038561562624
-7389844038561562624
-7389844038561562624
-7389844038561562624




Aucun commentaire:

Enregistrer un commentaire