override.md 3.2 KB
Newer Older
HansBug's avatar
HansBug 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
# 函数的重写

我们需要编写一个函数`gaussian`,使其生成服从正态分布$N\left(\mu, \sigma^2\right)$的随机数。其中$\mu$为数学期望,$\sigma^2$为方差,关于正态分布可以参考[百度百科](https://baike.baidu.com/item/%E6%AD%A3%E6%80%81%E5%88%86%E5%B8%83/829892)。现已有函数如下:

```java
import java.util.Random;

public class TestMain {
    private static final Random random = new Random();
    private static double gaussian(double mu, double sigma2) {
        return Math.sqrt(sigma2) * random.nextGaussian() + mu;
    }
}
```

但上述函数仍然需要我们传入$\mu$和$\sigma^2$的值,这会在实际使用这一函数的时候带来麻烦。

为了解决这一问题,我们需要对函数进行重写,使之能快速产生:

1. 数学期望$\mu = 0$的正态分布随机数
2. 服从标准正态分布($\mu = 0$,$\sigma^2 = 1$)的随机数

以达成简化`gaussian`函数使用的效果,以下选项正确的是?

## 答案

```java
import java.util.Random;

public class TestMain {
    private static final Random random = new Random();

    private static double gaussian() {
        return gaussian(0, 1);
    }

    private static double gaussian(double sigma2) {
        return gaussian(0, sigma2);
    }

    private static double gaussian(double mu, double sigma2) {
        return Math.sqrt(sigma2) * random.nextGaussian() + mu;
    }
}
```

## 选项

### Python味的默认值,然而Java并不支持

```java
import java.util.Random;

public class TestMain {
    private static final Random random = new Random();
    private static double gaussian(double mu = 0, double sigma2 = 1) {
        return Math.sqrt(sigma2) * random.nextGaussian() + mu;
    }
}
```

### 用法正确,但不符合题目要求

```java
import java.util.Random;

public class TestMain {
    private static final Random random = new Random();

    private static double gaussian() {
        return gaussian(0, 1);
    }

    private static double gaussian(double mu) {
        return gaussian(mu, 1);
    }

    private static double gaussian(double mu, double sigma2) {
        return Math.sqrt(sigma2) * random.nextGaussian() + mu;
    }
}
```

### Python味的默认值,Java并不支持,就一个也不成

```java
import java.util.Random;

public class TestMain {
    private static final Random random = new Random();

    private static double gaussian() {
        return gaussian(0, 1);
    }

    private static double gaussian(double mu, double sigma2 = 1) {
        return Math.sqrt(sigma2) * random.nextGaussian() + mu;
    }
}
```

### 函数重写时的标识符必须可区分

```java
import java.util.Random;

public class TestMain {
    private static final Random random = new Random();

    private static double gaussian() {
        return gaussian(0, 1);
    }

    private static double gaussian(double sigma2) {
        return gaussian(0, sigma2);
    }

    private static double gaussian(double mu, double sigma2) {
        return Math.sqrt(sigma2) * random.nextGaussian() + mu;
    }

    private static double gaussian(double sigma2, double mu) {
        return Math.sqrt(sigma2) * random.nextGaussian() + mu;
    }
}
```