Java,go,c# 相同隨機種子產生同樣隨機數算法

Java代碼:

public class MyRandom {
private long seed;
private int min;
private int max;
private ArrayList<integer> pool; //備用選擇池子
private long roundInt; //循環次數
private HashMap<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> this.pool.add(i);
}
if(this.roundInt+1<integer.max> 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.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;
}
}
/<integer.max>/<this.max>/<long>/<integer>

go代碼:

package funDir
import (
"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> 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.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;
}
/<int64>/<math.maxint32>/<this.max>

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> dicSetupObject=new Dictionary<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> 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.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;
}
}
/<int.maxvalue>/<this.max>/<long>/<long>

三種不同編程語言,隨機種子一樣,依次參數隨機數一樣。大部分用於前後端隨機數驗證。

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

Java,go,c# 相同隨機種子產生同樣隨機數算法


分享到:


相關文章: