Greater chance to roll 6 with dice in Java - java

I was wondering how can I program a dice which has a greater chance to roll a 6?
I've tried everything but I just cant figure the algorithm.
The probability of six is given by the user.
I just don't now how to program random which uses the given probability to roll a 6 or 1-5.

I would suggest using a percentage based scheme. let the user pick the probability of a six, in this example lets say 30%. Then choose a random number between 0-1.0 (which is what java's Random#nextFloat does). If its below or equal to .3, then make it a six, otherwise make it a 1-5.
Random r = new Random();
float probability = r.nextFloat(); // get a value between 0 and 1
if (probability < probabilityOfSix){
return 6;
} else {
return r.nextInt(4) +1;
}

Let's say a 6 has twice the probability to appear. Get a random number from 1 through 7, if your result is either a 6 or a 7, then you have a 6.
Same thing for three times the probability. Fetch a random number from 1-8: 6, 7, and 8 become a 6.

Divide what remains of the interval (0,1) when you take out a region the size of the probability of six into 5 equal pieces and assign these regions to 1 - 5:
/**
* #param pSix probability that a six is returned
* #param rnd Random instance
* #return a random integer between 1 and 6 with 1-5 equiprobable and P(6) = pSix
*/
public int loadedDice(final double pSix, final Random rnd) {
final double pOther = (1d - pSix) / 5d;
final float val = rnd.nextFloat();
if (val < pSix) {
return 6;
}
return (int) Math.ceil((val - pSix) / pOther);
}
}

Related

Generate random numbers with different probabilities within given ranges

Is there a simple algorithm that will print results of rolling a die such that the probability of getting 1,2,3,4,5 is 1/9 and the probability of getting a 6 is 3/9.
I would like to implement this in Java and intentionally only use Math.random(), if statements, for/ while loops.
As others suggested to make the sum of all events equal to 1, then number 9 will have a probability of 4/9 to be chosen.
Generate a random number between 1 and 9, inclusive on both ends. If the number be 1 to 5, you rolled that number, otherwise, you rolled 6. Note that there are 4 chances in this scheme to roll a 6, and 5 total chances to roll 1 through 5.
Random random = new Random();
int roll = random.nextInt(9) + 1; // create a random number
if (roll > 5) {
System.out.println("You rolled a 6");
}
else {
System.out.println("You rolled a " + roll);
}
To simulate more dice rolls you can add the above logic inside a for loop that runs for as many loops as you want.
Generating values for random variables with a certain distribution usually works like this:
You have a function which generates a random 0 <= q < 1,
You apply the quantile function and you obtain the value of your variable.
In your case you have a discrete random variable. You need an instance of Random:
private static final Random random = new Random();
the values assumed by the variable:
private static final double[] values = {1.0, 2.0, 3.0, 4.0, 5.0, 6.0};
Compute the cumulative distribution function (sum of probabilities of the values up to the specific value) for these values:
private static final double[] cdf = {1.0 / 9, 2.0 / 9, 3.0 / 9, 4.0 / 9, 5.0 / 9, 1.0};
You random generating function will return the last value for which the cdf is not greater than q:
public static double randomValue() {
double q = random.nextDouble();
for (int i = 0; i < values.length; i++) {
if (q > cdf[i]) continue;
return values[i];
}
throw new IllegalStateException();
}
Seems pretty straightforward:
// Pass in an instance of class Random
public static int gen(Random r) {
int i = r.nextInt(9); // uniformly generate 0,...,8 inclusive
if (i < 5) {
return i + 1; // returns 1,...,5 w/ probability 1/9
} else {
return 6; // returns 6 w/ probability 4/9
}
}
Warning, I no longer have a Java compiler on my machine, so I haven't compiled this. However, the algorithm is valid as confirmed in another language.

generate random number larger than given number until infinity in java

I want to generate a random number between a given number and infinity in Java.
I know this way to generate random number between 0 and 100:
int rand = (int) (Math.random() * 100 + (0));
But I need to generate a random number larger than 0 until infinity:
int rand = (int) (Math.random() * infinity + (m + 1));
There's no such thing as infinity in programming, but you can use Integer.MAX_VALUE which is basically the same.
You can read about it here: https://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html

Generating random numbers that are twice as likely to put out even numbers

I'm wondering if there was a way to create a random number generator that generates a number between two integers, but is twice as likely to generate an even number than an odd number. At current, I haven't come up with a way that's even similar or close to 2x as likely.
Simple but should work:
store random float call (0.0f - 1.0f) (random.nextFloat())
get a random integer in desired range
if random float call was less than 0.67f, if needed decrement or increment the random integer to make it even, return value
else, if needed decrement or increment the random integer to make it odd, return value
Make sure you decrement or increment towards the right direction if random integer is a boundary value of the desired range.
There are many ways you could do this. One would be to generate two integers: one between the user's bounds, and one between 0 and 2, inclusive. Replace the last bit of the first number with the last bit of the second number to get a result that is even twice as often as it is odd.
You do need to watch out for the possibility that the bit-twiddling last step puts the result out of bounds; in that event, you should re-draw from the beginning.
Implementing #SteveKuo 's suggestion in the comments:
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Please enter the minimum number that can be generated: ");
int min = scanner.nextInt();
System.out.print("Please enter the maximum number that can be generated: ");
int max = scanner.nextInt();
int evenOrOdd = 0 + (int)(Math.random() * ((2 - 0) + 1));
int random = 0;
if(evenOrOdd == 2) { // generate random odd number
if(max % 2 == 0) { --max; }
if(min % 2 == 0) { ++min; }
random = min + 2*(int)(Math.random() * ((max - min)/2+1));
} else { //get random number between [(min+1)/2, max/2] and multiply by 2 to get random even number between min and max
random = ((min+1)/2 + (int)(Math.random() * ((max/2 - (min+1)/2) + 1))) * 2;
}
System.out.printf("The generated random number is: %d", random);
}
}
Try it here!

Java Biasing Random Numbers in a Triangular Array

This question is an extension of Java- Math.random(): Selecting an element of a 13 by 13 triangular array. I am selecting two numbers at random (0-12 inclusive) and I wanted the values to be equal.
But now, since this is a multiplication game, I want a way to bias the results so certain combinations come up more frequently (like if the Player does worse for 12x8, I want it to come up more frequently). Eventually, I would like to bias towards any of the 91 combinations, but once I get this down, that should not be hard.
My Thoughts: Add some int n to the triangular number and Random.nextInt(91 + n) to bias the results toward a combination.
private int[] triLessThan(int x, int[] bias) { // I'm thinking a 91 element array, 0 for no bias, positive for bias towards
int i = 0;
int last = 0;
while (true) {
int sum = 0;
for (int a = 0; a < i * (i + 2)/2; a++){
sum += bias[a]
}
int triangle = i * (i + 1) / 2;
if (triangle + sum > x){
int[] toReturn = {last,i};
return toReturn;
}
last = triangle;
i++;
}
}
At the random number roll:
int sum = sumOfArray(bias); // bias is the array;
int roll = random.nextInt(91 + sum);
int[] triNum = triLessThan(roll);
int num1 = triNum[1];
int num2 = roll - triNum[0]; //now split into parts and make bias[] add chances to one number.
where sumOfArray just finds the sum (that formula is easy). Will this work?
Edit: Using Floris's idea:
At random number roll:
int[] bias = {1,1,1,...,1,1,1} // 91 elements
int roll = random.nextInt(sumOfBias());
int num1 = roll;
int num2 = 0;
while (roll > 0){
roll -= bias[num2];
num2++;
}
num1 = (int) (Math.sqrt(8 * num2 + 1) - 1)/2;
num2 -= num1 * (num1 + 1) / 2;
You already know how to convert a number between 0 and 91 and turn it into a roll (from the answer to your previous question). I would suggest that you create an array of N elements, where N >> 91. Fill the first 91 elements with 0...90, and set a counter A to 91. Now choose a number between 0 and A, pick the corresponding element from the array, and convert to a multiplication problem. If the answer is wrong, append the number of the problem to the end of the array, and increment A by one.
This will create an array in which the frequencies of sampling will represent the number of times a problem was solved incorrectly - but it doesn't ever lower the frequency again if the problem is solved correctly the next time it is asked.
An alternative and better solution, and one that is a little closer to yours (but distinct) creates an array of 91 frequencies - each initially set to 1 - and keeps track of the sum (initially 91). But now, when you choose a random number (between 0 and sum) you traverse the array until the cumulative sum is greater then your random number - the number of the bin is the roll you choose, and you convert that with the formula derived earlier. If the answer is wrong you increment the bin and update the sum; if it is right, you decrement the sum but never to a value less than one, and update the sum. Repeat.
This should give you exactly what you are asking: given an array of 91 numbers ("bins"), randomly select a bin in such a way that the probability of that bin is proportional to the value in it. Return the index of the bin (which can be turned into the combination of numbers using the method you had before). This function is called with the bin (frequency) array as the first parameter, and the cumulative sum as the second. You look up where the cumulative sum of the first n elements first exceeds a random number scaled by the sum of the frequencies:
private int chooseBin(float[] freq, float fsum) {
// given an array of frequencies (probabilities) freq
// and the sum of this array, fsum
// choose a random number between 0 and 90
// such that if this function is called many times
// the frequency with which each value is observed converges
// on the frequencies in freq
float x, cs=0; // x stores random value, cs is cumulative sum
int ii=-1; // variable that increments until random value is found
x = Math.rand();
while(cs < x*fsum && ii<90) {
// increment cumulative sum until it's bigger than fraction x of sum
ii++;
cs += freq[ii];
}
return ii;
}
I confirmed that it gives me a histogram (blue bars) that looks exactly like the probability distribution that I fed it (red line):
(note - this was plotted with matlab so X goes from 1 to 91, not from 0 to 90).
Here is another idea (this is not really answering the question, but it's potentially even more interesting):
You can skew your probability of choosing a particular problem by sampling something other than a uniform distribution. For example, the square of a uniformly sampled random variate will favor smaller numbers. This gives us an interesting possibility:
First, shuffle your 91 numbers into a random order
Next, pick a number from a non-uniform distribution (one that favors smaller numbers). Since the numbers were randomly shuffled, they are in fact equally likely to be chosen. But now here's the trick: if the problem (represented by the number picked) is solved correctly, you move the problem number "to the top of the stack", where it is least likely to be chosen again. If the player gets it wrong, it is moved to the bottom of the stack, where it is most likely to be chosen again. Over time, difficult problems move to the bottom of the stack.
You can create random distributions with different degrees of skew using a variation of
roll = (int)(91*(asin(Math.rand()*a)/asin(a)))
As you make a closer to 1, the function tends to favor lower numbers with almost zero probability of higher numbers:
I believe the following code sections do what I described:
private int[] chooseProblem(float bias, int[] currentShuffle) {
// if bias == 0, we choose from uniform distribution
// for 0 < bias <= 1, we choose from increasingly biased distribution
// for bias > 1, we choose from uniform distribution
// array currentShuffle contains the numbers 0..90, initially in shuffled order
// when a problem is solved correctly it is moved to the top of the pile
// when it is wrong, it is moved to the bottom.
// return value contains number1, number2, and the current position of the problem in the list
int problem, problemIndex;
if(bias < 0 || bias > 1) bias = 0;
if(bias == 0) {
problem = random.nextInt(91);
problemIndex = problem;
}
else {
float x = asin(Math.random()*bias)/asin(bias);
problemIndex = Math.floor(91*x);
problem = currentShuffle[problemIndex];
}
// now convert "problem number" into two numbers:
int first, last;
first = (int)((Math.sqrt(8*problem + 1)-1)/2);
last = problem - first * (first+1) / 2;
// and return the result:
return {first, last, problemIndex};
}
private void shuffleProblems(int[] currentShuffle, int upDown) {
// when upDown==0, return a randomly shuffled array
// when upDown < 0, (wrong answer) move element[-upDown] to zero
// when upDown > 0, (correct answer) move element[upDown] to last position
// note - if problem 0 is answered incorrectly, don't call this routine!
int ii, temp, swap;
if(upDown == 0) {
// first an ordered list:
for(ii=0;ii<91;ii++) {
currentShuffle[ii]=ii;
}
// now shuffle it:
for(ii=0;ii<91;ii++) {
temp = currentShuffle[ii];
swap = ii + random.nextInt(91-ii);
currentShuffle[ii]=currentShuffle[swap];
currentShuffle[swap]=temp;
}
return;
}
if(upDown < 0) {
temp = currentShuffle[-upDown];
for(ii = -upDown; ii>0; ii--) {
currentShuffle[ii]=currentShuffle[ii-1];
}
currentShuffle[0] = temp;
}
else {
temp = currentShuffle[upDown];
for(ii = upDown; ii<90; ii++) {
currentShuffle[ii]=currentShuffle[ii+1];
}
currentShuffle[90] = temp;
}
return;
}
// main problem posing loop:
int[] currentShuffle = new int[91];
int[] newProblem;
int keepGoing = 1;
// initial shuffle:
shuffleProblems( currentShuffle, 0); // initial shuffle
while(keepGoing) {
newProblem = chooseProblem(bias, currentShuffle);
// pose the problem, get the answer
if(wrong) {
if(newProblem > 0) shuffleProblems( currentShuffle, -newProblem[2]);
}
else shuffleProblems( currentShuffle, newProblem[2]);
// decide if you keep going...
}

Random Integer: Android

I'm guessing this is very simple, but for some reason I am unable to figure it out. So, how do you pick a random integer out of two numbers. I want to randomly pick an integer out of 1 and 2.
Just use the standard uniform random distribution, sample it, if it's less than 0.5 choose one value, if it's greater, choose the other:
int randInt = new Random().nextDouble() < 0.5 ? 1 : 2;
Alternatively, you can use the nextInt method which takes as input a cap (exclusive in the range) on the size and then offset to account for it returning 0 (the inclusive minimum):
int randInt = new Random().nextInt(2) + 1;
use following function:
int fun(int a, int b) {
Random r = new Random();
if(r.nextInt(2)) return a;
else return b;
}
This will return a or b with uniform distribution.
That means in a very simple way: If you run this function N times, expected occurrence of 'a' and 'b' are N/2 each.

Categories