不用Random类,自己重写一个java随机数生成方法
时间: 2024-01-14 20:02:55 浏览: 40
可以使用数学库中的随机数生成函数来生成伪随机数。下面是一个简单的例子:
```java
public class RandomNumberGenerator {
private long seed;
public RandomNumberGenerator(long seed) {
this.seed = seed;
}
public int nextInt(int n) {
seed = (seed * 1103515245 + 12345) % (1L << 31);
return (int) (seed % n);
}
}
```
这个随机数生成器使用了线性同余生成器(linear congruential generator)来生成伪随机数。随机数生成器的种子(seed)会影响生成的随机数序列。这个例子中的种子是一个long类型的整数,可以在构造函数中传入。
nextInt(n)方法会返回一个0到n-1之间的整数。它使用了一个简单的算法来生成随机数。如果需要更好的随机数质量,可以使用更复杂的算法,或者调用Java内置的Random类。
相关问题
java.定义一个子任务类,实现Runnable接口: 1.1 定义一个ArrayList集合对象,用于存放子线程的名称; 1.2 定义一个变量,用来记录正在运行的线程个数; 1.3定义一个构造方法,把进程总数作为参数进来; 1.4 在构造方法中,给正在运行的线程个数赋值为线程总数; 1.5 重写Runnable接口的run方法 ; 1.6 使用同步代码块,保证同一时间只能向集合中添加一个线程名称; 1.7 正在运行的线程个数减1; 1.8 所有的线程运行结束后遍历集合,打印所有线程的名称; 2.定义一个测试类: 2.1 在main方法中使用Random得到一个1到10的随机数; 2.2 调用子任务类的构造方法创建子任务对象,把随机数传递给构造方法; 2.3 循环创建n的子线程对象,并开启子线程;
下面是代码实现:
子任务类:
```java
import java.util.ArrayList;
public class SubTask implements Runnable {
private ArrayList<String> threadNames;
private int runningThreadCount;
public SubTask(int threadCount) {
threadNames = new ArrayList<String>();
runningThreadCount = threadCount;
}
@Override
public void run() {
synchronized (threadNames) {
threadNames.add(Thread.currentThread().getName());
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
runningThreadCount--;
if (runningThreadCount == 0) {
for (String name : threadNames) {
System.out.println(name);
}
}
}
}
```
测试类:
```java
import java.util.Random;
public class Test {
public static void main(String[] args) {
Random random = new Random();
int threadCount = random.nextInt(10) + 1;
SubTask subTask = new SubTask(threadCount);
for (int i = 0; i < threadCount; i++) {
Thread thread = new Thread(subTask);
thread.start();
}
}
}
```
测试结果:
随机生成的线程数为 4 时,输出结果为:
```
Thread-0
Thread-1
Thread-2
Thread-3
```
随机生成的线程数为 8 时,输出结果为:
```
Thread-0
Thread-1
Thread-2
Thread-3
Thread-4
Thread-5
Thread-6
Thread-7
```
4. 多线程问题。(15分) 假如关羽和张飞两人参加比武招亲,满足如下要求者获胜 (1)两人依次出拳,计算出拳的耗时; (2)一共5回合,总耗时少的获胜。 提示: (1)关羽和张飞分别为一个子线程; (2)耗时:可用 100 以内随机数生成:(提示:Math.randomo方法可返回 。-1以内的随机数) (3)生成一个Yongshi 类包括如下属性:姓名,时间;方法:输出西数。 (4)输出效果:
这是一个Java多线程问题,可以利用Thread类和Random类实现。下面是一个简单的实现示例:
```java
import java.util.Random;
class Yongshi {
private String name;
private int time;
public Yongshi(String name, int time) {
this.name = name;
this.time = time;
}
public String toString() {
return name + "出拳耗时" + time + "秒";
}
public int getTime() {
return time;
}
}
public class WuLinMeng {
public static void main(String[] args) {
Thread guanyu = new Thread(() -> {
System.out.println("关羽开始出拳...");
Random random = new Random();
int total = 0;
for (int i = 1; i <= 5; i++) {
int time = random.nextInt(100) + 1;
total += time;
System.out.println("第" + i + "回合:" + new Yongshi("关羽", time));
}
System.out.println("关羽出拳结束,总耗时:" + total + "秒");
});
Thread zhangfei = new Thread(() -> {
System.out.println("张飞开始出拳...");
Random random = new Random();
int total = 0;
for (int i = 1; i <= 5; i++) {
int time = random.nextInt(100) + 1;
total += time;
System.out.println("第" + i + "回合:" + new Yongshi("张飞", time));
}
System.out.println("张飞出拳结束,总耗时:" + total + "秒");
});
guanyu.start();
zhangfei.start();
try {
guanyu.join();
zhangfei.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
int guanyuTime = guanyu.getTime();
int zhangfeiTime = zhangfei.getTime();
if (guanyuTime < zhangfeiTime) {
System.out.println("关羽获胜!");
} else if (guanyuTime > zhangfeiTime) {
System.out.println("张飞获胜!");
} else {
System.out.println("平局!");
}
}
}
```
在这个示例中,我们创建了一个Yongshi类来存储选手姓名和出拳耗时,并重写了它的toString方法。每个选手都是一个独立的线程,使用Random类生成每一回合的出拳耗时,并将它们累加得到总耗时。最后,通过调用Thread类的join方法来确保两个线程都执行完毕,然后比较它们的总耗时来决定胜负。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![java](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)