简体   繁体   English

如何在随机生成器中获得 50/50 的机会

[英]How to get a 50/50 chance in random generator

I am trying to get a 50/50 chance of get either 1 or 2 in a random generator.我试图在随机生成器中获得 1 或 2 的 50/50 机会。

For example:例如:

Random random = new Random();
int num = random.nextInt(2)+1;

This code will output either a 1 or 2.此代码将输出 1 或 2。

Let's say I run it in a loop:假设我循环运行它:

for ( int i = 0; i < 100; i++ ) {
    int num = random.nextInt(2)+1 ;
}

How can I make the generator make an equal number for 1 and 2 in this case?在这种情况下,如何使生成器为 1 和 2 生成相同的数字?

So I want this loop to generate 50 times of number 1 and 50 times of number 2.所以我希望这个循环生成 1 号的 50 次和 2 号的 50 次。

一种方法:用五十个 1 和五十个 2 填充ArrayList<Integer> ,然后在其上调用Collection.shuffle(...)

50/50 is quite easy with Random.nextBoolean() 50/50 使用Random.nextBoolean()很容易

private final Random random = new Random();

private int next() {
  if (random.nextBoolean()) {
    return 1;
  } else {
    return 2;
  }
}

Test Run:测试运行:

final ListMultimap<Integer, Integer> histogram = LinkedListMultimap.create(2);
for (int i = 0; i < 10000; i++) {
    nal Integer result = Integer.valueOf(next());
  histogram.put(result, result);
}
for (final Integer key : histogram.keySet()) {
  System.out.println(key + ": " + histogram.get(key).size());
}

Result:结果:

1: 5056
2: 4944

You can't achieve this with random .你不能用random来实现这一点。 If you need exactly 50 1s and 50 2s, you should try something like this:如果你正好需要 50 个 1 和 50 个 2,你应该尝试这样的事情:

int[] array = new int[100];
for (int i = 0; i < 50; ++i)
 array[i] = 1;
for (int i = 50; i < 100; ++i)
 array[i] = 2;

shuffle(array); // implement shuffling algorithm or use an already existing one

EDIT: I understand that if you are looking to accomplish exactly 50-50 results, then my answer was not accurate.编辑:我知道如果您希望完成 50-50 个结果,那么我的回答是不准确的。 You should use a pre-filled collection, since it is impossible to achive that using any kind of randomness.您应该使用预先填充的集合,因为使用任何类型的随机性都无法实现。 This considered, my answer is still valid for the title of the question, so, this is it:考虑到这一点,我的回答对于问题的标题仍然有效,所以,就是这样:

Well, you do not need the rnd generator to do this.好吧,您不需要 rnd 生成器来执行此操作。 Comming from javascript, I would go with a single liner:来自 javascript,我会选择一个班轮:

return Math.random() > 0.5 ? 1: 2;

Explanation: Math.random() returns a number between 0(inclusive) and 1(exclusive), so, we just examine weather is larger than 0.5 (middle value).说明: Math.random()返回一个介于 0(包含)和 1(不包含)之间的数字,因此,我们只检查天气是否大于 0.5(中间值)。 In theory there is a 50% change that does.理论上有 50% 的变化。

For a more generic use, you can just replace 1:2 to true:false对于更通用的用途,您可以将1:2替换为true:false

You can adjust the probability along the way so that the probability of getting a one decreases as you get more ones.您可以在此过程中调整概率,以便获得一个的概率随着获得更多的概率而降低。 This way you don't always have a 50% chance of getting a one, but you can get the result you expected (exactly 50 ones):这样,您并不总是有 50% 的机会获得一个,但您可以获得预期的结果(正好 50 个):

int onesLeft = 50;

for(int i=0;i<100;i++) {
  int totalLeft = 100 - i;
  // we need a probability of onesLeft out of (totalLeft)
  int r = random.nextInt(totalLeft);
  int num;
  if(r < onesLeft) {
    num = 1;
    onesLeft --;
  } else {
    num = 2;
  }
}

This has an advantage over shuffling because it generates numbers incrementally so it desn't need memory to store the numbers.这比改组有优势,因为它以增量方式生成数字,因此不需要内存来存储数字。

You have already successfully created a random generator that returns 1 or 2 with equal probability.您已经成功创建了一个随机生成器,它以相等的概率返回12

As (many) other's have mentioned, your next request, to force an exact 50/50 distributions in 100 trials, does not fall in line with random number generation.正如(许多)其他人所提到的,您的下一个请求,即在 100 次试验中强制执行精确的50/50 分布,与随机数生成不符。 As shown in https://math.stackexchange.com/questions/12348/probability-of-getting-50-heads-from-tossing-a-coin-100-times , the realistic expectation of that occurring is only around 8%.https://math.stackexchange.com/questions/12348/probability-of-getting-50-heads-from-tossing-a-coin-100-times所示,发生这种情况的现实预期仅为 8% 左右. So even while you might expect 50 of each, that exact outcome is actually rather rare.因此,即使您可能期望每个有 50 个,但确切的结果实际上是相当罕见的。

The Law of Large Numbers states that you should close in on expected value as your number of trials increases.大数定律指出,随着试验次数的增加,您应该接近预期值。

So for your actual question: How can I make the generator make an equal number for 1 and 2 in this case?因此,对于您的实际问题:在这种情况下,如何让生成器为 1 和 2 生成相同的数字?

The best (humorous) answer I can come up with is: "Run it in an infinite loop."我能想到的最好(幽默)的答案是:“无限循环地运行它。”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM