使用随机数作为参数来生成随机数

时间:2016-11-07 12:48:04

标签: c# random numbers generated

我不确定这是一个可接受的帖子,但出于好奇心

public static void main(String[] args) {

    Method1();
    Method2();
    Method3();

}

private static void Method3() {
    ArrayList<Object> a = new ArrayList<>();
    ArrayList<Object> b = new ArrayList<>();

    for (int i = 0; i < Math.pow(2, 15); i++) {
        a.add("a" + i);
    }
    for (int i = 0; i < Math.pow(2, 15); i++) {
        b.add("b" + i);
    }

    ArrayList<Object> aa = new ArrayList<>(a);
    a.ensureCapacity(2*aa.size());
    a.clear();

    long time1 = System.currentTimeMillis();


    for (int i = 0; i<b.size();i++) {
        a.add(aa.get(i));
        a.add(b.get(i));
    }
    long time2 = System.currentTimeMillis();

    System.out.println(time2 - time1);
}

private static void Method2() {
    ArrayList<Object> a = new ArrayList<>();
    ArrayList<Object> b = new ArrayList<>();

    for (int i = 0; i < Math.pow(2, 15); i++) {
        a.add("a" + i);
    }

    for (int i = 0; i < Math.pow(2, 15); i++) {
        b.add("b" + i);
    }

    long time1 = System.currentTimeMillis();

    int size = a.size();
    a.add(b.get(0));
    for (int i = 1; i < size; i++) {
        a.add(a.get(i));
        a.add(b.get(i));
    }
    a.subList(1, size).clear();

    long time2 = System.currentTimeMillis();

    System.out.println(time2 - time1);

}

private static void Method1() {
    ArrayList<Object> a = new ArrayList<>();
    ArrayList<Object> b = new ArrayList<>();

    for (int i = 0; i < Math.pow(2, 15); i++) {
        a.add("a" + i);
    }

    for (int i = 0; i < Math.pow(2, 15); i++) {
        b.add("b" + i);
    }

    long time1 = System.currentTimeMillis();

    a.add(1, b.get(0));
    for (int i = 1; i < b.size(); i++) {
        a.add((2 * i + 1), b.get(i));
    }

    long time2 = System.currentTimeMillis();

    System.out.println(time2 - time1);
}

更多的是随机数,而不仅仅是说

Random rnd = new Random();
        int random1 = rnd.Next(1, 24);
        int random2 = rnd.Next(25, 49);

        int random3 = rnd.Next(random1, random2);

        int random4 = rnd.Next(50, 74);
        int random5 = rnd.Next(75, 100);

        int random6 = rnd.Next(random4, random5);

        int random7 = rnd.Next(random3, random6);
        Console.WriteLine(random7);

3 个答案:

答案 0 :(得分:1)

您的问题假定存在一定程度的随机性。这是不正确的,随机性是二元状态。如果无法准确预测试验结果,则试验随机。否则我们说它是 deterministic 。通过类比,你会问一个更死的问题,有人被枪杀或被电刑杀死的人?死了就死了!(*)

我们用分布来描述随机性,这些分布描述了各种结果的相对可能性。例如,均匀,高斯,三角形,泊松或指数分布,仅举几例。它们都会产生不同的结果在不同范围内下降的可能性,但我知道没有概率论说均匀分布比高斯分布更随机,反之亦然。同样地,你的两个算法会产生不同的结果分布,但由于两者都不可预测,因此它们都是随机的。

如果你想捕捉可预测性的程度,你可能应该问哪个算法具有更高的entropy而不是更随机的算法。众所周知的结果是均匀分布在有界区间支持的分布类中具有最大熵。因此,您的复杂算法具有比简单均匀分布更低的熵,并且更具可预测性。

(*) - 除了“公主新娘”,韦斯利只是“大部分时间死了”。

答案 1 :(得分:0)

第一种方法产生更像弯曲分布而不是线性分布的东西。

尝试运行以下命令行应用,您将看到不同之处:

using System;

namespace Demo
{
    class Program
    {
        const int N = 1000000;

        static void Main()
        {
            var result1 = testRandom(randomA);
            var result2 = testRandom(randomB);

            Console.WriteLine("Results for randomA():\n");
            printResults(result1);

            Console.WriteLine("\nResults for randomB():\n");
            printResults(result2);
        }

        static void printResults(int[] results)
        {
            for (int i = 0; i < results.Length; ++i)
            {
                Console.WriteLine(i + ": " + new string('*', (int)(results[i]*2000L/N)));
            }
        }

        static int[] testRandom(Func<Random, int> gen)
        {
            Random rng = new Random(12345);

            int[] result = new int[100];

            for (int i = 0; i < N; ++i)
                ++result[gen(rng)];

            return result;
        }

        static int randomA(Random rng)
        {
            return rng.Next(1, 100);
        }

        static int randomB(Random rnd)
        {
            int random1 = rnd.Next(1, 24);
            int random2 = rnd.Next(25, 49);

            int random3 = rnd.Next(random1, random2);

            int random4 = rnd.Next(50, 74);
            int random5 = rnd.Next(75, 100);

            int random6 = rnd.Next(random4, random5);

            return rnd.Next(random3, random6);
        }
    }
}

答案 2 :(得分:0)

简易测试直方图)将显示实际分布

private static Random rnd = new Random();

private static int[] Hist() {
  int[] freqs = new int[100];

  // 100 buckets, 1000000 samples; we might expect about 10000 values in each bucket
  int n = 1000000;

  for (int i = 0; i < n; ++i) {
    int random1 = rnd.Next(1, 24);
    int random2 = rnd.Next(25, 49);

    int random3 = rnd.Next(random1, random2);

    int random4 = rnd.Next(50, 74);
    int random5 = rnd.Next(75, 100);

    int random6 = rnd.Next(random4, random5);

    int random7 = rnd.Next(random3, random6);

    freqs[random7] = freqs[random7] + 1;
  }

  return freqs;
}

...

Console.Write(string
 .Join(Environment.NewLine, Hist()
   .Select((v, i) => $"{i,2}: {v,5}");

你会得到像

这样的东西
 0:      0 <- OK, zero can't appear
 1:     21 <- too few (about 10000 expected)
 2:     56 <- too few (about 10000 expected)
 3:    125 ...
 4:    171
 5:    292
 6:    392
 7:    560
 8:    747 ...
 9:    931 <- too few (about 10000 expected)
 ...
 45: 21528 <- too many (about 10000 expected)
 46: 21549 ...
 47: 21676
 48: 21699
 49: 21432
 50: 21692
 51: 21785
 52: 21559
 53: 21047
 54: 20985 ...
 55: 20820 <- too many (about 10000 expected)
 ...
 90:   623 <- too few (about 10000 expected)
 91:   492 ...
 92:   350
 93:   231
 94:   173
 95:    88
 96:    52
 97:    13
 98:     0 ...
 99:     0 <- too few (about 10000 expected)

没有像均匀分布的随机值,远离它,而是一种钟形曲线