动手动脑

X(n+1) = (a * X(n) + c) mod mModulus=231-1=int.MaxValue Multiplier=75=16807 C=0 当显示过231-2个数之后,才可能重复。 动手动脑: 编写一个方法,使用以上算法生成指定数目(比如1000个)的随机整数。

1
2
3
4
5
6
7
8
9
10
11
12
public class RandomnNumberGenerator {
    private static final long MODULUS = (long) Integer.MAX_VALUE;
    private static final long MULTIPLIER = 16807;
    private static final long CONSTANT = 0;
 
    public static long[] generateRandomNumbers(int count) {
        long[] randomNumbers = new long[count];
        long seed = 1;
 
        for (int i = 0; i < count; i++) {
            seed = (MULTIPLIER * seed + CONSTANT) % MODULUS;
            randomNumbers[i] = seed;//限制在1000以内(randomNumbers[i] = seed % 1000;)  }     return randomNumbers; } public static void main(String[] args) {   int count = 1000;   long[] randomNumbers = generateRandomNumbers(count); // 输出生成的随机数     for (long number : randomNumbers) {     System.out.println(number);   }   } }

  这样的随机数都很大,在源代码中我有注释如何修改。

 

观察观察下行代码,有什么特殊的地方?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public class MethodOverload {
 
    public static void main(String[] args) {
        System.out.println("The square of integer 7 is " + square(7));
        System.out.println("\nThe square of double 7.5 is " + square(7.5));
    }
 
    public static int square(int x) {
        return x * x;
    }
 
    public static double square(double y) {
        return y * y;
    }
}

  定义了两个名为 square 的方法,一个接受一个整数参数,另一个接受一个双精度浮点数参数。这两个方法的名称相同,但参数类型不同。

posted @   *太¥^白%  阅读(367)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· 清华大学推出第四讲使用 DeepSeek + DeepResearch 让科研像聊天一样简单!
· 实操Deepseek接入个人知识库
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库
点击右上角即可分享
微信分享提示