mardi 4 mai 2021

random number generation in Java from Power law distribution

I am trying to generate random numbers in a particular range (1.0 to 10.0), the numbers should follow a power law distribution. I have followed this (Random number generator that produces a power-law distribution?) accepted solution. My code is working relatively well even though the logic may not look too good ;). My question is, what is the significance of "distribution power n"? Can I control how many numbers are generated in a particular range using this n? My code is given below, if you can suggest more sophisticated logic, it will be very helpful:

public class GeneratePowerLaw {

    public static void main(String[] args) {

        List<Double> list1to2 = new ArrayList<Double>();
        List<Double> list3to4 = new ArrayList<Double>();
        List<Double> list5to6 = new ArrayList<Double>();
        List<Double> list7to8 = new ArrayList<Double>();
        List<Double> list9to10 = new ArrayList<Double>();

        double x, x1, n, x0, y, a, b, c, d, e, f, g;
        Random r = new Random();
        x0 = 1.0;
        x1 = 10.0;
        n = -2.5;

        for (int i = 0; i < 20; i++) {
            y = r.nextDouble();
            System.out.println("value of random number from unifor dist" + y);

            // below is formula for generating power law distributed numbers
            // from stakcoverflow
            // x = [(x1^(n+1) - x0^(n+1))*y + x0^(n+1)]^(1/(n+1))

            a = Math.pow(x1, n + 1);
            b = Math.pow(x0, n + 1);
            c = b * y;
            d = Math.pow(x0, n + 1);
            e = (1 / (n + 1));

            f = a - c + d;
            x = Math.pow(f, e);
            System.out.println("Number Power law" + x);

            if (x >= 0.0 && x <= 2.9999999999999) {
                list1to2.add(x);
            }

            if (x >= 3.0 && x <= 4.9999999999999) {
                list3to4.add(x);
            }

            if (x >= 5.0 && x <= 6.9999999999999) {
                list5to6.add(x);
            }

            if (x >= 7.0 && x <= 8.9999999999999) {
                list7to8.add(x);
            }

            if (x >= 9.0 && x <= 10.9999999999999) {
                list9to10.add(x);
            }

        }
        System.out.println("list1to2 size" + list1to2.size());
        System.out.println("list3to4 size" + list3to4.size());
        System.out.println("list5to6 size" + list5to6.size());
        System.out.println("list7to8 size" + list7to8.size());
        System.out.println("list9to10 size" + list9to10.size());

    }

}



Aucun commentaire:

Enregistrer un commentaire