lundi 2 novembre 2020

Java Random with seed always starts with same number

I've tried to search the web for this behaviour but nothing so far, the issue is the following:

when using the following code:

    Random r2  = new Random(1);
    
    for(int p = 0; p < 25; p++) {
        r2 = new Random(p);
        System.out.println();
        for(int i = 0; i < 15; i++)
            System.out.print(Math.round(r2.nextFloat()*100) + " ");
        }

this will always return:

73 83 24 61 64 31 55 12 60 78 33 25 39 61 98 
73 10 41 41 21 4 33 66 97 71 1 15 96 16 94 
73 29 90 0 50 85 99 3 86 92 99 45 23 52 7 
73 56 7 81 7 58 77 58 23 85 66 35 81 7 3 
73 91 92 81 92 22 68 28 8 50 2 85 70 80 80 
73 18 9 61 49 94 46 83 45 43 70 75 28 35 76 
73 37 58 21 78 76 11 20 34 64 68 5 54 71 89 
73 64 75 1 35 49 90 74 71 57 35 95 12 25 85 
73 98 60 2 20 13 81 45 56 22 72 45 1 99 63 
73 25 77 82 77 85 59 99 93 14 39 35 59 53 58 
73 45 26 41 6 67 24 37 82 36 37 65 86 89 71 
73 71 43 21 63 40 3 91 19 29 4 55 43 44 67 
73 6 28 22 48 4 94 62 4 93 41 5 33 17 45 
73 33 44 2 5 76 72 16 41 86 8 95 91 72 40 
73 52 94 62 34 58 37 54 30 7 6 25 17 8 54 
73 79 11 42 91 31 16 8 67 0 74 15 75 63 49 
73 53 53 79 51 67 3 45 67 91 57 85 13 87 70 
73 80 70 59 8 40 82 99 4 84 24 75 71 41 66 
73 99 19 19 37 22 47 36 93 6 22 5 97 78 79 
73 26 36 99 94 95 25 91 30 99 89 95 55 32 75 
73 60 21 99 79 58 16 61 16 63 26 45 44 5 52 
73 87 38 79 36 31 95 16 53 56 93 35 2 60 48 
73 6 87 39 65 13 60 53 41 77 91 65 29 96 61 
73 33 4 19 22 86 38 7 78 70 59 55 86 51 57 
73 68 88 20 8 49 29 78 64 35 95 5 76 24 34 

as you can see, the same number is always the same, and if I continue with a longer list, it will only change between 2 numbers 72/73, is there any reason this could be happening?

regards, Jakes




Aucun commentaire:

Enregistrer un commentaire