From 5 random to 7 random and extended __

Source: Internet
Author: User

From 5 random to 7 random and its extended public class randomint{public static int rand1to5 () {return (int) (Math.random () *5) +1;
    	}//Generate 1-7 random function public static int Rand1to7 () {//return rand1to5 () +2;
    	int num=0;
    	do{num= (Rand1to5 ()-1) *5+rand1to5 ()-1;
    	}while (NUM>20);
    return num%7+1;
    	 //************************************************************* public static int rand01p () {//Free to change P
    	 Double p=0.83;
    Return Math.random () <p?0:1;
    	 public static int rand01 () {int num=0;
    	 do{num=rand01p ();
    	 }while (num==rand01p ());
    return num;
    public static int Rand0to3 () {return rand01 () *2+rand01 ();
    	}//Generate 1-6 random function public static int rand1to6 () {int num=0;

    	do{Num=rand0to3 () *4+rand0to3 ();
    	
    	}while (NUM&GT;11);
    	
    return num%6+1; }//************************************************************ Public StatIC int rand1tom (int m) {return (int) (Math.random () *m) +1;
		}//Generate 1-n random function public static int Rand1ton (int n, int m) {int[] Nmsys = Getmsysnum (n-1, M);
		int[] Randnum = GETRANMSYSNUMLESSN (Nmsys, M);
	Return Getnumfrommsysnum (Randnum, m) + 1;
		(=//convert value to M-binary number public static int[] Getmsysnum (int value, int m) {int[] res = new INT[32];
		int index = RES.LENGTH-1;
			while (value!= 0) {res[index--] = value% m;
		value = value/m;
	return res;
	}//equal probability randomly produces a 0~nmsys range of numbers, but is a M-system expression.
		public static int[] GETRANMSYSNUMLESSN (int[] nmsys, int m) {int[] res = new Int[nmsys.length];
		int start = 0;
		while (nmsys[start] = = 0) {start++;
		int index = start;
		Boolean lastequal = true;
			while (index!= nmsys.length) {Res[index] = Rand1tom (m)-1;
					if (lastequal) {if (Res[index] > Nmsys[index]) {index = start;
					Lastequal = true;
				Continue
				else {lastequal = Res[index] = = Nmsys[index]; }} INDex++;
	return res;
		///Convert the number of M into 10 binary public static int getnumfrommsysnum (int[] msysnum, int m) {int res = 0;
		for (int i = 0; I!= msysnum.length i++) {res = res * m + msysnum[i];
	return res; }//************************************************************** public static void Main (String[]args) {System.out
        . println (Rand1to7 ());
        System.out.println (Rand1to6 ());
	System.out.println (Rand1ton (4,5)); }
}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.