Java: Random
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 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 | /** * 版权所有 2023 涂聚文有限公司 * 许可信息查看: * 描述: *用100元买100只鸡,大公鸡5元一只,母鸡3元1只,小鸡一元3只,问各能买多少只? * 历史版本: JDK 8.01 * 2023-03-12 创建者 geovindu * 2023-03-12 添加 Lambda * 2023-03-12 修改:date * 接口类 * 2023-03-12 修改者:Geovin Du * 生成API帮助文档的指令: *javadoc - -encoding Utf-8 -d apidoc RandomHelper.java * Interface * Record * Annotation * Enum * */ package Common; import java.util.Random; /** * * @author geovindu 凃聚文,塗聚文,Geovin Du * @version 1.0 * * 隨機數生成 * * * */ public class RandomHelper { /** * * */ static Random random= new Random(); static String randomChars = "ABCDFGHJKMNPQRTVWXYabcdefghjkmnpqrtvwxy23456789!#@%$&" ; /** * * @param max 随大值 * @param min 最小值 * @return * */ public static int RandomInt( int min, int max) { int num=random.nextInt(max-min+ 1 )+min; return num; } /** *指定字符串自定義長度 * @param lengh 自定長度 * @return 返回需要的字符串 * */ public static String RandomString( int lengh) { String getCode= "" ; StringBuilder stringBuilder= new StringBuilder(); for ( int i = 0 ; i <lengh ; i++) { int num=random.nextInt(randomChars.length()); stringBuilder.append(randomChars.charAt(num)); } getCode=stringBuilder.toString(); return getCode; } /** * * @param lengh 自定義抽取几個 * * */ public static String RandomArryString( int lengh) { String getCode= "" ; String [] arryStr= new String[] { "Broccoli" , "SiboDu" , "Kiwi" , "Kale" , "Toma" , "GeovinDu" , "塗聚文" }; StringBuilder stringBuilder= new StringBuilder(); for ( int i = 0 ; i <lengh ; i++) { int num=random.nextInt(arryStr.length); stringBuilder.append(arryStr[num]); } getCode=stringBuilder.toString(); return getCode; } /** * 自定義字符串和長度 * @param lengh 自定長度 * @param strMore 自定字符串 * @return 返回需要的字符串 * * */ public static String RandomString( int lengh,String strMore) { String getCode= "" ; StringBuilder stringBuilder= new StringBuilder(); for ( int i = 0 ; i <lengh ; i++) { int num=random.nextInt(strMore.length()); stringBuilder.append(strMore.charAt(num)); } getCode=stringBuilder.toString(); return getCode; } /** * * @param lengh 自定義抽取几個 * @param arryStr * @return * */ public static String RandomArryString( int lengh,String [] arryStr) { String getCode= "" ; //String [] arryStr=new String[] {"Broccoli", "SiboDu", "Kiwi", "Kale", "Toma","GeovinDu","塗聚文"}; StringBuilder stringBuilder= new StringBuilder(); for ( int i = 0 ; i <lengh ; i++) { int num=random.nextInt(arryStr.length); stringBuilder.append(arryStr[num]); } getCode=stringBuilder.toString(); return getCode; } } /** * 版权所有 2023 涂聚文有限公司 * 许可信息查看: * 描述: *用100元买100只鸡,大公鸡5元一只,母鸡3元1只,小鸡一元3只,问各能买多少只? * 历史版本: JDK 8.01 * 2023-03-12 创建者 geovindu * 2023-03-12 添加 Lambda * 2023-03-12 修改:date * 接口类 * 2023-03-12 修改者:Geovin Du * 生成API帮助文档的指令: *javadoc - -encoding Utf-8 -d apidoc RandomDal.java * Interface * Record * Annotation * Enum * */ package Dal; import Common.RandomHelper; /** * * * * */ public class RandomDal { /** * * @return * */ public String getRandomString() { //生成字符串位數隨機 int lengh=RandomHelper.RandomInt( 6 , 10 ); //六位至十位之閒 return RandomHelper.RandomString(lengh); } /** * * @param lengh * @return * */ public String getRandomString( int lengh) { return RandomHelper.RandomString(lengh); } /** * * @return * */ public int getRandomInt() { return RandomHelper.RandomInt( 0 , 10 ); } /** * * @param min * @param max * @return * */ public int getRandomInt( int min, int max) { return RandomHelper.RandomInt(min,max); } /** * * @return * */ public String getRandomArryStr() { return RandomHelper.RandomArryString( 1 ); } /** * * @param count * @return * * */ public String getRandomArryStr( int count) { return RandomHelper.RandomArryString(count); } /** * * @param count * @param arry * @return * * */ public String getRandomArryStr( int count,String [] arry) { return RandomHelper.RandomArryString(count,arry); } } /** * 版权所有 2023 涂聚文有限公司 * 许可信息查看: * 描述: *用100元买100只鸡,大公鸡5元一只,母鸡3元1只,小鸡一元3只,问各能买多少只? * 历史版本: JDK 8.01 * 2023-03-12 创建者 geovindu * 2023-03-12 添加 Lambda * 2023-03-12 修改:date * 接口类 * 2023-03-12 修改者:Geovin Du * 生成API帮助文档的指令: *javadoc - -encoding Utf-8 -d apidoc RandomBll.java * Interface * Record * Annotation * Enum * */ package Bll; import Dal.RandomDal; /** * * * * */ public class RandomBll { /** * * * */ RandomDal randomDal= new RandomDal(); /** * * @return * */ public String getRandomString() { String str=randomDal.getRandomString(); return str; } /** * * @return ; * */ public int getRandomInt() { int num=randomDal.getRandomInt(); return num; } /** * * @param min * @param max * @return * */ public int getRandomInt( int min, int max) { int num=randomDal.getRandomInt(min,max); return num; } /** * * * * */ public String getRandomArryStr() { return randomDal.getRandomArryStr(); } } |
调用:
1 2 3 4 5 6 7 8 | //隨機數 RandomBll randomBll= new RandomBll(); for ( int i = 0 ; i < 10 ; i++) { System.out.println( "int:" +randomBll.getRandomInt()); System.out.println( "int(6-10):" +randomBll.getRandomInt( 6 , 10 )); System.out.println( "string:" +randomBll.getRandomString()); System.out.println( "抽獎:" +randomBll.getRandomArryStr()); } |
输出:
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 | int : 9 int ( 6 - 10 ): 9 string:j5Ntq!c 抽獎:SiboDu int : 0 int ( 6 - 10 ): 6 string:hD3Wg4FVBQ 抽獎:Broccoli int : 8 int ( 6 - 10 ): 6 string:m78378w9c 抽獎:Broccoli int : 7 int ( 6 - 10 ): 8 string:RRQKTk6T 抽獎:GeovinDu int : 5 int ( 6 - 10 ): 7 string:tR5Q6qdBvj 抽獎:Kiwi int : 10 int ( 6 - 10 ): 10 string:t8KtNer 抽獎:Kiwi int : 3 int ( 6 - 10 ): 10 string:tWA8Tgqc 抽獎:Broccoli int : 5 int ( 6 - 10 ): 10 string:g4q8e#xgyN 抽獎:Toma int : 10 int ( 6 - 10 ): 6 string:!NhddKb! 抽獎:Toma int : 1 int ( 6 - 10 ): 7 string:c8WQvN97NX 抽獎:Toma [ 1 , 3 , 5 , 7 , 9 , 11 , 13 , 15 , 17 , 19 , 21 , 23 , 25 , 27 , 29 , 31 , 33 , 35 , 37 , 39 , 41 , 43 , 45 , 47 , 49 , 51 , 53 , 55 , 57 , 59 , 61 , 63 , 65 , 67 , 69 , 71 , 73 , 75 , 77 , 79 , 81 , 83 , 85 , 87 , 89 , 91 , 93 , 95 , 97 , 99 ] 1 -- 100 以内的奇数的和是: 2500 [ 5 , 10 , 15 , 20 , 25 , 30 , 35 , 40 , 45 , 50 , 55 , 60 , 65 , 70 , 75 , 80 , 85 , 90 , 95 , 100 ] 1 -- 100 以内除五的数的和是: 1050 输入 10 个整数(按回车输入另一个数): |
哲学管理(学)人生, 文学艺术生活, 自动(计算机学)物理(学)工作, 生物(学)化学逆境, 历史(学)测绘(学)时间, 经济(学)数学金钱(理财), 心理(学)医学情绪, 诗词美容情感, 美学建筑(学)家园, 解构建构(分析)整合学习, 智商情商(IQ、EQ)运筹(学)生存.---Geovin Du(涂聚文)
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
2017-04-14 FullCalendar – jQuery Event Calendar in ASP.NET
2012-04-14 csharp 復制DataTable修改某列的值
2010-04-14 SQL Server 2005 Recursion(递归) and WITH Clause