Saturday 15 May 2010

probability - Using random number generator to generate a number with a given expected value -


I came to the following question:

Using the rand () function, generate the expected value number Do k The options are:

1)

  int GetRandom (int k) {v = 0; While (rand () <1.0f / (float) a) v ++; Return vi; }   

2)

  int GetRandom (int k) {v = 0; While (rand ()   

3)

  int GetRandom (int k) {v = 0; While (rand ()> (1-1.0 f / (float) (k + 1)) v ++; Return vi; }   

1) The correct answer seemed like. Investigating the result for specific values ​​of k indicates that this is not the case. (I set k = 3 . In the image below, the frequency distribution of values ​​for 100000 tests can be seen

you want (2 ) A k with these samples.

A geometrical distribution indicates such an experiment:

  • A certain event will result in a result Along with that which happens either 0 or 1
  • The result of an incident is 1 probability with p and 0 What is the index of the first event with the result of 1-p
  • 1 with the possibility?

    So if X ~ G (p) , where X is a random variable and the possibility of p is above, then represents X "What is the index of the first event with the result of 1?" Hopefully e [x] = 1 / p .

    Looking at this information, it should be clear now That of the following with the random variable X with p = 1 / k (and (2a) Equivalent)).

      int sample (int k) {int v = 1; While (true) {// the result is true with probability p = 1 / k bool result = rand () & lt; 1 / (double) K; If (results) return v; Other V ++; }}   

    Keep in mind that seeing peak (mode) and distributing is not the same thing as the peak of geometrical distribution will always be at 1!

No comments:

Post a Comment