龙空技术网

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

坏桃子 138

前言:

眼前同学们对“java怎么取随机数”大致比较注重,同学们都想要剖析一些“java怎么取随机数”的相关内容。那么小编在网络上收集了一些对于“java怎么取随机数””的相关资讯,希望兄弟们能喜欢,小伙伴们快快来学习一下吧!

Java代码:

public class MyRandom { private long seed; private int min; private int max; private ArrayList<Integer> pool; //备用选择池子 private long roundInt; //循环次数 private HashMap<Long,Long> 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<this.max;i++){ this.pool.add(i); } if(this.roundInt+1<Integer.MAX_VALUE) { this.roundInt++; }else { this.roundInt=0; } } int selectIndex =(int)(this.seed%100*(long)(this.max-this.min)*(long)(this.pool.size())+this.getRoundArgs()*this.getSum+(long)(this.pool.size()/8*(int)((this.max-this.min)/2))); 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.size(); return pool.remove(selectIndex); } //修改变量 private void changeGetSum(){ if(this.getSum<(this.max-this.min)/3) { this.getSum+=(long) ((this.max-this.min)/28.9f*8f); }else { this.getSum+=(long) ((this.max-this.min)/64f); } } //打破周期循环 private long getRoundArgs(){ long reInt; if(this.dicSetupObject.containsKey(this.roundInt)){ reInt=this.dicSetupObject.get(this.roundInt); }else { int remInt=(int)(this.roundInt%30); if(remInt==1 || remInt==3){ reInt=(this.roundInt-3034)*25; }else if(remInt==0){ reInt=3034*23*7; }else if(remInt==2|| remInt==23){ reInt=((this.roundInt%24)-343)*10; }else if(remInt==4|| remInt==29){ reInt=((this.roundInt%85)*3)/9; }else if(remInt==5|| remInt==20 || remInt==25) { reInt = ((this.roundInt % 33) * 3) % 90; }else if(remInt==6 ) { reInt = this.seed % 37; }else if(remInt==8|| remInt==11 || remInt==15) { reInt = this.seed % 31 + this.roundInt % 37 * 3; }else if(remInt==10 || remInt==18) { reInt = this.seed / 245 - this.roundInt % 33 + 345; }else if(remInt==12 || remInt==16 || remInt==22) { reInt = this.seed / 8 % 21; }else if(remInt==17 || remInt==24 ){ reInt=this.seed%1000*((this.roundInt/234)%63); }else if(remInt==26 || remInt==27){ reInt=(this.seed/this.roundInt)%23*358; }else { reInt = this.roundInt; } } return reInt; }}
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<this.max;i++{ this.pool=append(this.pool,i); } if(this.roundInt+1 <math.MaxInt32){ this.roundInt++; }else { this.roundInt=0; } } var selectIndex int32=int32((this.seed%100*int64(this.max-this.min)*int64(len(this.pool))+this.getRoundArgs()*this.getSum+int64(len(this.pool)/8*int((this.max-this.min)/2)))); for 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%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<int64(this.max-this.min)/3) { this.getSum+=int64(float64(this.max-this.min)/float64(28.9)*float64(8)); }else { this.getSum+=int64(float64(this.max-this.min)/float64(64)); }}//打破周期循环func (this *MyRandom) getRoundArgs() int64 { var reInt int64; value,isContainState:=this.dicSetupObject[this.roundInt] if(isContainState){ reInt=int64(value); }else { var remInt int=(int)(this.roundInt%30); if(remInt==1 || remInt==3){ reInt=int64((this.roundInt-3034)*25); }else if(remInt==0){ reInt=3034*23*7; }else if(remInt==2|| remInt==23){ reInt=int64(((this.roundInt%24)-343)*10); }else if(remInt==4|| remInt==29){ reInt=int64(((this.roundInt%85)*3)/9); }else if(remInt==5|| remInt==20 || remInt==25) { reInt =int64 (((this.roundInt % 33) * 3) % 90); }else if(remInt==6 ) { reInt = this.seed % 37; }else if(remInt==8|| remInt==11 || remInt==15) { reInt = this.seed % 31 + int64(this.roundInt) % 37 * 3; }else if(remInt==10 || remInt==18) { reInt = this.seed / 245 - int64(this.roundInt) % 33 + 345; }else if(remInt==12 || remInt==16 || remInt==22) { reInt = this.seed / 8 % 21; }else if(remInt==17 || remInt==24 ){ reInt=this.seed%1000*((int64(this.roundInt)/234)%63); }else if(remInt==26 || remInt==27){ reInt=this.seed/int64(this.roundInt)%23*358; }else { reInt = int64(this.roundInt); } } return reInt;}
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<long,long> dicSetupObject=new Dictionary<long,long>(); 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<this.max;i++){ this.pool.Add(i); } if(this.roundInt+1<int.MaxValue) { this.roundInt++; }else { this.roundInt=0; } } long args=this.getRoundArgs(); int selectIndex =(int)(this.seed%100*(long)(this.max-this.min)*(long)(this.pool.Count)+this.getRoundArgs()*this.getSum+(long)(this.pool.Count/8*(int)((this.max-this.min)/2))); Debug.Log(this.getSum+"=>"+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<(this.max-this.min)/3) { this.getSum+=(long) ((this.max-this.min)/28.9f*8f); }else { this.getSum+=(long) ((this.max-this.min)/64f); } } //打破周期循环 private long getRoundArgs(){ long reInt; if(this.dicSetupObject.ContainsKey(this.roundInt)){ this.dicSetupObject.TryGetValue(this.roundInt,out reInt); }else { int remInt=(int)(this.roundInt%30); if(remInt==1 || remInt==3){ reInt=(this.roundInt-3034)*25; }else if(remInt==0){ reInt=3034*23*7; }else if(remInt==2|| remInt==23){ reInt=((this.roundInt%24)-343)*10; }else if(remInt==4|| remInt==29){ reInt=((this.roundInt%85)*3)/9; }else if(remInt==5|| remInt==20 || remInt==25) { reInt = ((this.roundInt % 33) * 3) % 90; }else if(remInt==6 ) { reInt = this.seed % 37; }else if(remInt==8|| remInt==11 || remInt==15) { reInt = this.seed % 31 + this.roundInt % 37 * 3; }else if(remInt==10 || remInt==18) { reInt = this.seed / 245 - this.roundInt % 33 + 345; }else if(remInt==12 || remInt==16 || remInt==22) { reInt = this.seed / 8 % 21; }else if(remInt==17 || remInt==24 ){ reInt=this.seed%1000*((this.roundInt/234)%63); }else if(remInt==26 || remInt==27){ reInt=(this.seed/this.roundInt)%23*358; }else { reInt = this.roundInt; } } return reInt; }}

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

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

标签: #java怎么取随机数