mercredi 6 septembre 2017

Algorithm of Random number generators & Modifying the frequency of it [on hold]

I'm wondering exactly what sort of algorithm Random generators use. I'm more interested in that of Java, as its what I'm using currently, but any random number generator will suffice. Does it use system time as a base? So if I revert the time and execute the same command at precisely the same moment, will I get the same "random" numbers?

The second part ties into the first, as I'm using a random number generator for a practice program:

    int a[] = new int[21];
    //fills array.
    for (int i = 1; i < 21; i++)
    {
        a[i] = i;
    }
    int b[] = new int[21];
    //Array for random ints.
    Random rd = new Random();
    //loop to pick out parts of array 'a' at random and change its value.
    for (int rand = 1; rand < 21; rand++)
    {
        b[rand] = rd.nextInt((20 - 1) + 1);
        a[b[rand]] = 0;
    }

and using the import java.util.Random; function, when I create an array of Random integers, it is indeed random (seemingly. As true random cannot be achieved by a computer) but for my purposes it is too frequent; I have an array of 20 ints filled with 1's and I'm trying to replace random parts of the array with 0's. The way I coded it works for my purposes, only there are too many 0's. If I can't quite use and modify the algorithm of the Random generator myself, how should I go about reducing the frequency of the zeros generated? Thanks in advance!

p.s. It's late and I'm tired, so if any part of the question(s) is confusing or makes no sense, you know why. Try to extrapolate :D




Aucun commentaire:

Enregistrer un commentaire