等概率平均随机算法-平均随机值班算法
等概率平均随机算法
当使用Java的Random函数获取随机数时,比如随机生成1~10数字,随机10次后,并不一定每个数都出现。如果我们在每天同一时刻,比如每天早上9点触发,随机生成1~10数字,因为Random函数使用的随机种子是当前系统时间的毫秒数,如果每天同一时刻、连毫秒数也一样,调用随机函数,则有很大概率会随机到同一个数。所以Random函数是一个伪随机算法,并不是真正的随机算法。
爱因斯坦曾经说过:"世界上并不存在真正的随机算法"。因为所有的随机算法都需要一个随机种子,然后再根据随机种子进行各种计算,最后封装成一个随机算法。要保证随机算法随机,就要保证随机种子随机。而随机种子,都是在一定条件下产生的,故世界上不存在真正的随机算法。如果你能写出一个真正的随机算法,那么可能就获得诺贝尔奖了,以后别忘了我,哈哈哈。
那么如果想要随机生成1~10数字,随机10次后,每个数都出现一次,每次都是随机产生一个数,怎么实现呢?就比如值班,6个人值班,每天随机生成一个值班人,6天内每个人都会被轮到一次,每次都是随机生成一个人,怎么实现呢?
代码如下:
1 import com.alibaba.fastjson.JSON; 2 import com.springboot.springbootduty.common.response.Response; 3 import com.springboot.springbootduty.common.utils.DateFormatUtil; 4 import lombok.extern.slf4j.Slf4j; 5 import org.apache.commons.collections.CollectionUtils; 6 import org.springframework.web.bind.annotation.GetMapping; 7 import org.springframework.web.bind.annotation.RequestMapping; 8 import org.springframework.web.bind.annotation.RestController; 9 10 import java.util.ArrayList; 11 import java.util.Arrays; 12 import java.util.Date; 13 import java.util.List; 14 import java.util.Random; 15 16 /** 17 * @author linliquan 18 * @description: 等概率平均随机算法-平均随机值班算法 19 * @create 2022/2/23 10:00 20 */ 21 @Slf4j 22 @RestController 23 @RequestMapping("/duty") 24 public class Test { 25 26 27 final static List<String> LIST = new ArrayList<>(Arrays.asList("张三", "李四", "王五", "赵六", "孙七", "周八")); 28 29 static List<String> dutyList = new ArrayList<>(); 30 31 /** 32 * 获取今日值班人 33 * @return 34 */ 35 @GetMapping("/getTodayDuty") 36 public Response<String> getTodayDuty(){ 37 if (CollectionUtils.isEmpty(dutyList)) { 38 log.info("#######################################"); 39 dutyList.addAll(LIST); 40 log.info("初始的 dutyList:{}", JSON.toJSON(dutyList)); 41 return Response.succ(getTodayRandomDuty()); 42 } 43 log.info("有数据的 dutyList:{}", JSON.toJSON(dutyList)); 44 return Response.succ(getTodayRandomDuty()); 45 } 46 47 /** 48 * 随机获取今日值班人 49 * @return 50 */ 51 private String getTodayRandomDuty() { 52 int i = new Random().nextInt(dutyList.size()); 53 String todayDutyName = dutyList.get(i); 54 String date = DateFormatUtil.convertStringByDateFormat(new Date()); 55 log.info("今日值班人:{}, 值班日期:{}, 随机数:{}", todayDutyName, date, i); 56 // 使用迭代器删除dutyList,防止数组越界 57 dutyList.removeIf(todayDutyName::equals); 58 log.info("删除后的 dutyList:{}", JSON.toJSON(dutyList)); 59 log.info("******************************************"); 60 return todayDutyName; 61 } 62 }
相关的依赖:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> 4 <modelVersion>4.0.0</modelVersion> 5 <parent> 6 <groupId>org.springframework.boot</groupId> 7 <artifactId>spring-boot-starter-parent</artifactId> 8 <version>2.6.2</version> 9 <relativePath/> <!-- lookup parent from repository --> 10 </parent> 11 <groupId>com.springboot</groupId> 12 <artifactId>springboot-duty</artifactId> 13 <version>0.0.1-SNAPSHOT</version> 14 <name>springboot-duty</name> 15 <description>springboot-duty</description> 16 <properties> 17 <java.version>1.8</java.version> 18 <fastjson-version>1.2.75</fastjson-version> 19 <apache-commons-lang3-version>3.7</apache-commons-lang3-version> 20 <apache-commons-collections-version>3.2.2</apache-commons-collections-version> 21 <apache-commons-codec-version>1.14</apache-commons-codec-version> 22 <apache-commons-lang-version>2.6</apache-commons-lang-version> 23 </properties> 24 <dependencies> 25 <dependency> 26 <groupId>org.springframework.boot</groupId> 27 <artifactId>spring-boot-starter-web</artifactId> 28 </dependency> 29 <dependency> 30 <groupId>org.mybatis.spring.boot</groupId> 31 <artifactId>mybatis-spring-boot-starter</artifactId> 32 <version>2.2.1</version> 33 </dependency> 34 35 <dependency> 36 <groupId>com.alibaba</groupId> 37 <artifactId>druid</artifactId> 38 <version>1.1.11</version> 39 </dependency> 40 41 <dependency> 42 <groupId>org.springframework.boot</groupId> 43 <artifactId>spring-boot-starter-test</artifactId> 44 <scope>test</scope> 45 </dependency> 46 47 <dependency> 48 <groupId>mysql</groupId> 49 <artifactId>mysql-connector-java</artifactId> 50 <version>8.0.23</version> 51 </dependency> 52 53 <!-- idea需要安装lombok插件 --> 54 <dependency> 55 <groupId>org.projectlombok</groupId> 56 <artifactId>lombok</artifactId> 57 <version>1.18.16</version> 58 </dependency> 59 60 <dependency> 61 <groupId>com.alibaba</groupId> 62 <artifactId>fastjson</artifactId> 63 <version>${fastjson-version}</version> 64 </dependency> 65 66 <dependency> 67 <groupId>org.apache.commons</groupId> 68 <artifactId>commons-lang3</artifactId> 69 <version>${apache-commons-lang3-version}</version> 70 </dependency> 71 72 <dependency> 73 <groupId>commons-collections</groupId> 74 <artifactId>commons-collections</artifactId> 75 <version>${apache-commons-collections-version}</version> 76 </dependency> 77 78 <dependency> 79 <groupId>commons-codec</groupId> 80 <artifactId>commons-codec</artifactId> 81 <version>${apache-commons-codec-version}</version> 82 </dependency> 83 84 <dependency> 85 <groupId>commons-lang</groupId> 86 <artifactId>commons-lang</artifactId> 87 <version>${apache-commons-lang-version}</version> 88 </dependency> 89 90 </dependencies> 91 92 <build> 93 <plugins> 94 <plugin> 95 <groupId>org.springframework.boot</groupId> 96 <artifactId>spring-boot-maven-plugin</artifactId> 97 <version>2.6.2</version> 98 </plugin> 99 </plugins> 100 </build> 101 102 </project>