"Java" secret how to use unequal probability random number to make 10 bet nine cheat gambling size game, please do not indulge in various games

Source: Internet
Author: User



The last time the "Java" produced a different random number in a certain range (click the open link) explained how to generate random numbers, and then said how to produce a certain range of different random numbers, but these are equal probabilities to produce random problems. Equal probability under normal circumstances, more should be said to be sufficient in the academic, but in real life, especially in the game must now play yellow gambling poison of the background, such as the probability of generating random numbers is far from enough. You as a game business, said the bad point is the dealer, must not wait for the probability to generate random numbers to circle the player's money. For example, if you can't make it synthetic, it's 50%, the synth is 50%, you have to set the synth to 1% or even smaller. This way you can let the player through Murphy's law to give you a lot of manpower, material resources. The so-called Murphy's Law in probability theory has: that is, small probability events through a large number of tests will inevitably occur, popular point, is "saints thousand worry, there must be a loss." A fool is a god, and one must be. ”






I. BASIC OBJECTIVES



Set a ten bet nine cheat gambling size game, unequal probability to produce large and small results.



1, first, this is a very simple game, considering the small part has not been exposed to the good child of yellow gambling poison, or introduction: User input 0 for the bet "small", 1 for "big", then randomly generated three 1-6 integer, if the three number equal is "pass kill", regardless of the user bet what IS is to lose , if the three number and in the 3-10, then is "small", if 11-18 is "big", compared with the results of the user guess, see whether the user guessed.






Of course, we also take into account your random input situation, the program is robust, you enter nothing will crash.






2, if the program is normal to write, very simple, randomly produce 3 numbers, total judgment on the line, but, in order to let the player unknowingly consume money, the program must not write this, if the player guess "small", you have 1-3 chance to adjust a little bit, if the player guess "big", you produce 4-6 probability of a small point, So can be like the result, if the player played 50 times, all bets "small", he can only win 8 times:






3, if the player plays 1000 times, all bets "big", it can only win 361 times:






4, join the player to play 200,000 times, all bets "big", it can only win 70,000 times






Anyway, in summary, the player is relatively, certainly cannot win the banker.






Second, the basic idea



The player killed all can not win the banker, that is, the game maker is always the core idea of making money: the game produced to the player's favorable event probability to adjust less, of course, you can not adjust too little, too few players think you are lying, circle money, although you are indeed in the circle of money, but also to do unknowingly circle money.






Third, the production process



1, the first to deal with the player input information, we require the player must enter 0 and 1, then to exclude other results, initialize a n, used to receive input from the player, this is used in the Java input and output stream, is the textbook on the basis of things, if you can not understand, Just take it as a C-scanf and remember it. Then define an array to hold the results. Used to store randomly generated numbers every time.





System.out.println ("Bet size games, enter 0 for small, enter 1 for large");
int n = 0;
try {
n = Integer.parseInt (new BufferedReader (new InputStreamReader (
System.in)). ReadLine ());
} catch (Exception e) {
System.out.println ("N must be a positive integer");
return;
}
if (n <0 || n> 1) {
System.out.println ("N is 0 and 1");
return;
}
if (n == 0) {
System.out.println ("You guessed" small "");
} else {
System.out.println ("You guessed" big "");
}
int resultArr [] = new int [3];
2, then is the core of the whole program, how to generate each result according to the probability of the user input, here use the probability theory of the two distribution of ideas. If the user enters 0, which is small, then the resulting result, 70% probability is 4-6,new random (). Nextint (3) + 4; Capable of generating a 4-6 integer, which produces a different random number in "Java" within a certain range (Click to open the link) has been explained that the other parts of their complementary, no longer repeat. The main say is how to set up 70% of the resulting result is 4-6. Before you produce the results, take advantage of the new random (). Nextdouble () generates a random number, the random number is the equal probability of the decimal between 0-1, the probability of the decimal less than 0.7 is certainly 70%, then it means that there is a 70% probability of entering this if, only 30% The probability of entering the else. You can do a lot of things by using this random number and then nesting the events. Actually also on a postgraduate examination, even participated in the probability of the final exam students all understand the two distribution, no big deal.







switch (n) {
case 0:
	for (int i = 0; i < 3; i++) {
		if (new Random().nextDouble() < 0.7) {
			int j = new Random().nextInt(3) + 4;
			resultArr[i] = j;
			System.out.print(j + " ");
		} else {
			int j = new Random().nextInt(3) + 1;
			resultArr[i] = j;
			System.out.print(j + " ");
		}
	}
	break;
case 1:
	for (int i = 0; i < 3; i++) {
		if (new Random().nextDouble() < 0.7) {
			int j = new Random().nextInt(3) + 1;
			resultArr[i] = j;
			System.out.print(j + " ");
		} else {
			int j = new Random().nextInt(3) + 4;
			resultArr[i] = j;
			System.out.print(j + " ");
		}
	}
	break;
}
3, after the judgment result, the settlement part, no big deal, Java, or even the foundation of the program design, is a bunch for and if, so why say conditional structure, loop structure, you can write well, you write good program. First of all to determine whether to kill, because the killing will not have to think about it, the size of the entire collection, here to traverse the whole array, not the variable to the last, traverse to the penultimate second can be. See if all is the last number equals the last number, if not it is not a pass kill. Instead of killing words, the result array is summed, and the summation here uses a new type of traversal after JDK1.5, a new array traversal method after "Java" with JDK1.5, and HashMap should not store multivariate groups (click Open link) already said, Don't dwell on it. Here's a new type of conditional structure?: Only one variable can be used, so it is not good to write, so the old-fashioned if and else. The player will win and vice versa if it is equal to the player's guess N.







boolean isAllIdentical = true;
for (int i = 0; i <resultArr.length-1; i ++) {
if (resultArr [i]! = resultArr [i + 1]) {
isAllIdentical = false;
break;
}
}
if (isAllIdentical) {
System.out.println ("Kill! You lost!");
} else {
int total = 0;
for (int e: resultArr) {
total + = e;
}
if (total <11) {
System.out.print ("Result: small!");
if (n == 0) {
System.out.println ("You won!");
} else {
System.out.println ("You lost!");
}
} else {
System.out.print ("Result: Big!");
if (n == 1) {
System.out.println ("You won!");
} else {
System.out.println ("You lost!");
}
}
}





At this point, the random number of unequal probability of the problem is done, the entire program connected with the result is as follows:





import java.util. *;
import java.io. *;

public class Random_Probability {
public static void main (String [] args) throws IOException {
System.out.println ("Bet size games, enter 0 for small, enter 1 for large");
int n = 0;
try {
n = Integer.parseInt (new BufferedReader (new InputStreamReader (
System.in)). ReadLine ());
} catch (Exception e) {
System.out.println ("N must be a positive integer");
return;
}
if (n <0 || n> 1) {
System.out.println ("N is 0 and 1");
return;
}
if (n == 0) {
System.out.println ("You guessed" small "");
} else {
System.out.println ("You guessed" big "");
}
int resultArr [] = new int [3];
switch (n) {
case 0:
for (int i = 0; i <3; i ++) {
if (new Random (). nextDouble () <0.7) {
int j = new Random (). nextInt (3) + 4;
resultArr [i] = j;
System.out.print (j + "");
} else {
int j = new Random (). nextInt (3) + 1;
resultArr [i] = j;
System.out.print (j + "");
}
}
break;
case 1:
for (int i = 0; i <3; i ++) {
if (new Random (). nextDouble () <0.7) {
int j = new Random (). nextInt (3) + 1;
resultArr [i] = j;
System.out.print (j + "");
} else {
int j = new Random (). nextInt (3) + 4;
resultArr [i] = j;
System.out.print (j + "");
}
}
break;
}
boolean isAllIdentical = true;
for (int i = 0; i <resultArr.length-1; i ++) {
if (resultArr [i]! = resultArr [i + 1]) {
isAllIdentical = false;
break;
}
}
if (isAllIdentical) {
System.out.println ("Kill! You lost!");
} else {
int total = 0;
for (int e: resultArr) {
total + = e;
}
if (total <11) {
System.out.print ("Result: small!");
if (n == 0) {
System.out.println ("You won!");
} else {
System.out.println ("You lost!");
}
} else {
System.out.print ("Result: Big!");
if (n == 1) {
System.out.println ("You won!");
} else {
System.out.println ("You lost!");
}
}
}

}

} 






"Java" secret how to use unequal probability random number to make 10 bet nine cheat gambling size game, please do not indulge in various games


Related Article

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.