java random产生随机数_Java,go,c# 相同随机种子产生同样随机数算法

Java代码:

public class MyRandom { private long seed; private int min; private int max; private ArrayList pool; //备用选择池子 private long roundInt; //循环次数 private HashMap dicSetupObject=new HashMap<>(); private long getSum ; //获取次数 private String dicSetup="1:981*2:42*3:566612*4:104*5:3040*6:340*7:56221*8:3405*9:4023*10:2467*11:5320*12:5*13:972*14:15*15:52*16:10976*17:97*18:3782921*19:342*20:78862*22:89*38:692*50:3*90:309*234:33*3245:348"; public MyRandom(long seed ,int min,int max) { this.seed = seed; this.min = min; this.max = max; this.pool = new ArrayList<>(max); this.roundInt=0; this.getSum=0; String[] dicSetupArray=this.dicSetup.split("[*]"); for (String dicSetupArrayStr:dicSetupArray){ String[] dicSetupArrayStrA=dicSetupArrayStr.split("[:]"); this.dicSetupObject.put(Long.valueOf(dicSetupArrayStrA[0]),Long.valueOf(dicSetupArrayStrA[1])); } } public long nextInt(){ this.changeGetSum(); if(pool.isEmpty()){ //生成备用池子 for (int i=this.min;i>17; }else if(i%4==2) { selectIndex=selectIndex>>3; }else { selectIndex=selectIndex<<10; } } if(selectIndex<0){ selectIndex=-selectIndex; } selectIndex=selectIndex%this.pool.size(); return pool.remove(selectIndex); } //修改变量 private void changeGetSum(){ if(this.getSum

go代码:

package funDirimport ( "math" "strconv" "strings")type Random interface { New(seed int64, min int32,max int32); NextInt() int64; getRoundArgs() int64;}type MyRandom struct { seed int64; min int32; max int32; getSum int64; //获取次数 pool []int32; //备用池子 roundInt int32; //循环次数 dicSetupObject map[int32]int32; //预设词典备用区 dicSetup string; //预设字典设置}func (this *MyRandom) New(seed int64, min int32,max int32) { this.seed=seed; this.min=min; this.max=max; this.dicSetup="1:981*2:42*3:566612*4:104*5:3040*6:340*7:56221*8:3405*9:4023*10:2467*11:5320*12:5*13:972*14:15*15:52*16:10976*17:97*18:3782921*19:342*20:78862*22:89*38:692*50:3*90:309*234:33*3245:348"; this.roundInt=0; var dicSetupArray []string=strings.Split(this.dicSetup, "*") this.dicSetupObject=map[int32]int32{}; this.pool=make([]int32,0); for _,v:= range dicSetupArray{ var dicSetupArrayStrA []string= strings.Split(v,":"); var ki,ke=strconv.Atoi(dicSetupArrayStrA[0]); var vi,ve=strconv.Atoi(dicSetupArrayStrA[1]); if(ke==nil && ve==nil) { this.dicSetupObject[int32(ki)]=int32(vi); } }}func (this *MyRandom) NextInt() int64 { this.changeGetSum(); if(len(this.pool)==0){ for i:=this.min;i>17; }else if(i%4==2) { selectIndex=selectIndex>>3; }else { selectIndex=selectIndex<<10; } } if(selectIndex<0){ selectIndex=-selectIndex; } selectIndex=selectIndex%int32(len(this.pool)); var getOne int32= this.pool[selectIndex]; this.pool=append(this.pool[0:selectIndex],this.pool[selectIndex+1:len(this.pool)]...); return int64(getOne);}//修改变量func (this *MyRandom) changeGetSum () { if(this.getSum

C#代码:

using System.Collections.Generic;using UnityEngine;using System.Collections;public class MyRandom { private long seed; private int min; private int max; private ArrayList pool; //备用选择池子 private long roundInt; //循环次数 private Dictionary dicSetupObject=new Dictionary(); private long getSum ; //获取次数 private string dicSetup="1:981*2:42*3:566612*4:104*5:3040*6:340*7:56221*8:3405*9:4023*10:2467*11:5320*12:5*13:972*14:15*15:52*16:10976*17:97*18:3782921*19:342*20:78862*22:89*38:692*50:3*90:309*234:33*3245:348"; public MyRandom(long seed ,int min,int max) { this.seed = seed; this.min = min; this.max = max; this.pool = new ArrayList(max); this.roundInt=0; this.getSum=0; string[] dicSetupArray=this.dicSetup.Split('*'); foreach (string dicSetupArrayStr in dicSetupArray){ string[] dicSetupArrayStrA=dicSetupArrayStr.Split(':'); this.dicSetupObject.Add(long.Parse(dicSetupArrayStrA[0]),long.Parse(dicSetupArrayStrA[1])); } } public long nextInt(){ this.changeGetSum(); if(pool.Count==0){ //生成备用池子 for (int i=this.min;i"+args+"=>"+selectIndex); for (int i = 0; i <4; i++) { if(i%4==0){ selectIndex=selectIndex<<13; }else if(i%4==1){ selectIndex=selectIndex>>17; }else if(i%4==2) { selectIndex=selectIndex>>3; }else { selectIndex=selectIndex<<10; } } if(selectIndex<0){ selectIndex=-selectIndex; } selectIndex=selectIndex%this.pool.Count; int getOne=(int)this.pool[selectIndex]; pool.RemoveAt(selectIndex);  return getOne; } //修改变量 private void changeGetSum(){ if(this.getSum

三种不同编程语言,随机种子一样,依次参数随机数一样。大部分用于前后端随机数验证。

使用注意:多线程下不安全。

d7ed109bc7abfc5bebfd599db6fd79e7.png
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值